MySQL select command with a column named "set" [duplicate] - mysql

This question already has answers here:
Select a column with a keyword name
(6 answers)
Closed 1 year ago.
I would like to select some parameters in a column of my database. The issue that I have is that the column where I want to select the data is named "set".
SELECT * FROM database.table where set=5130;
"Set" is also a keyword in MySQL which leads to an error :
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set=5130' at line 1
How should I bypass this issue ?

I guess changing the column name is not an option for you. Then you need to use backticks (`).
SELECT * FROM database.table where `set`=5130;

The best approach would probably be to rename the column. If this is not an option, you can escape it by surrounding it with backticks:
SELECT * FROM database.table WHERE `set` = 5130;
-- Here ---------------------------^---^

Related

how to rename a two-word(for example; 'manager id') column name in MySQL [duplicate]

This question already has answers here:
Alterning a table that has spaces in its name
(2 answers)
Rename a column in MySQL
(18 answers)
Closed last year.
i tried using this code below
ALTER TABLE table_name change "MANAGER ID" TO manager_id;
error message received:
1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"MANAGER ID" TO manager_id' at line 1 0.000 sec
Correct syntax is:
ALTER TABLE table_name RENAME COLUMN `MANAGER ID` TO manager_id;
According to this question and answer your are using the wrong quotes. It should be
`MANAGER ID`
See also the reference documentation.

IF NOT EXISTS in SQL query has this error : Unrecognized statement type. (near "IF NOT EXISTS" at position 0) [duplicate]

This question already has answers here:
How can I do 'insert if not exists' in MySQL?
(11 answers)
How can I fix MySQL error #1064?
(3 answers)
Closed 1 year ago.
im using this sql query :
IF NOT EXISTS(SELECT 1 FROM Payments WHERE CustomerID = '145300')
INSERT INTO Payments(CustomerID,Amount)
VALUES('145300',12.33)
but i get this error:
Error
Static analysis:
1 errors were found during analysis.
Unrecognized statement type. (near "IF NOT EXISTS" at position 0)
SQL query: Documentation
IF NOT EXISTS(SELECT 1 FROM Payments WHERE CustomerID = '145300') INSERT INTO Payments(CustomerID,Amount) VALUES('145300',12.33)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO Payments(CustomerID,Amount)
VALUES('145300',12.33)' at line 2
this SQL query which I am using is from the answer of the question in this link : sql query
based an another answer, I even used END IF at the end of this statement but it didn't work yet.
how can solve this problem?
You can use Flow Control Statements only in stored programs. Use INSERT IGNORE when inserting a row with an explicit PK value. Alternatively if CustomerID is not a PK by chance
insert Payments(CustomerID,Amount)
select *
from (
values row ('145300',12.33)
) t(CustomerID,Amount)
where not exists (select 1
from Payments p
where p.CustomerID = t.CustomerID);

Mysql error in my syntax [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
Hey i have an error in my mysql syntax, and i dont know why i get it. I can't see an error in my code and i have checked the web for help...
INSERT INTO downloads_log
(file,by,time) VALUES
(1,1, NOW())
The error:
'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'by, time) VALUES (1,1,NOW())
by,file and time are reserved keywords used in MYSQL. Use a back tick (`) to escape the keywords in your query. Change your query to :
INSERT INTO downloads_log
(`file`,`by`,`time`) VALUES
(1,1, NOW())
by is a reserved keyword. Try:
INSERT INTO downloads_log
(`file`, `by`, `time`) VALUES
(1, 1, NOW())
You're using reserved keywords as column names. Try to avoid using reserved keywords as column names, but if you haven't choice you have to pass them to backticks `` in following:
INSERT INTO downloads_log (`file`,`by`,`time`) VALUES (1,1, NOW())

mysql insert from select query returning syntax error [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
So, I run an online imageboard and I am updating to a newer imageboard software. Rather than manually enter the board information for each new imageboard, I wanted to just port over the fields I need from the old table to the new one, but PHPMYADMIN is giving me a mysql syntax error and I don't know what the problem is:
INSERT INTO `tryboards` (uri, title)
SELECT name, desc FROM `aasboards`;
This should move the data from the old table to the new, yes? The aasboards table has several columns I want to omit. The tryboards table contains 3 fields, the 3rd one being subtitle, which is nullable and shouldn't be needed for this query.
EDIT: the error is as follows:
Error
SQL query: Documentation
INSERT INTO tryboards ( uri, title )
SELECT name, DESC
FROM aasboards
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc FROM aasboards' at line 2
desc is a reserved word:
INSERT INTO `tryboards` (uri, title)
SELECT name, `desc` FROM `aasboards`;

unable to execute insert query using table called 'order'? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
Database name : test
table names : order, order_shipping,order_payment
The query below gives me error
INSERT INTO order(order_status,customer_id) values('booked',1)
error : 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order(order_status,customer_id) values('booked',1)' at line 1
But the exact same query will work if i add database name before tablename
INSERT INTO test.order(order_status,customer_id) values('booked',1)
result : insertion successfull
I renamed tablename 'order' to 'order_main' and it works without database name
INSERT INTO order_main(order_status,customer_id) values('booked',1)
insertion successfull
My question is why does not my original query work without database name attached to table name. Is it because I have other tables starting with this table name ???
table in my database : order, order_shipping,order_payment
order is a reserved keyword within MySQL. If you want to use it as an identifier, enclose it in backticks:
INSERT INTO `order` (order_status,customer_id) values('booked',1)
In the second query, you specify a full identifier, which MySQL does not mistake for a keyword. Hence, it works without problems.