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

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.

Related

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

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 ---------------------------^---^

MYSQL - Concatenate part of a column name and its index number

I would like to concatenate a column and its index (numerical name) inside of a SELECT statement in MySQL.
For example there are 4 columns: (Column1, Column2,Column3...ColumnN). Instead of updating the values by referring to them by name, I hope to use a variable named 'index' and attach it to the end of the word "Column".
e.g. (Column(index),Column(index)...)
I have tried the CONCAT() and CONCAT_WS functions from the MySQL documentation.
For example, if I set the value of the variable index = 3, it results in the following error:
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 '('Column','3')
The alternative would be to redesign database and its relations and I wish to avoid this.
Is what I am attempting possible for column names or simply strings within the database?
Edit:
$score_update = "UPDATE Users
SET CONCAT ('USER_module','$module_chosen_index') ='$module_quiz_score'
WHERE USER_firstname = '$username'";
The values of the $module_chosen_index can be (1,2...15) and the column title if I entered it and didn't use CONCAT() is USER_module3.

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`;

Making a backup copy of SQL table throws me this error: "#1064 - You have an error in your SQL syntax; "

I'm trying to make a backup of my table in MySql but I get this 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 'table `zbackup_oc_t_city` from `oc_t_city` LIMIT 0, 30' at line 1
This is the code that I'm using to backup
SELECT * INTO TABLE `zbackup_oc_t_city` FROM `oc_t_city`
Here is my oc_t_city table:
Here is zbackup_oc_t_city
I have tried it on numerous tables and it keeps throwing me the same error... any ideas?
Thanks
If you want to create your backup table and do the backup in just one statement use
CREATE TABLE `zbackup_oc_t_city` SELECT * FROM `oc_t_city`;
CREATE TABLE ... SELECT Syntax
You can create one table from another by adding a SELECT statement at
the end of the CREATE TABLE statement:
CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;
With MySQL you can't use SELECT ... INTO to select into a new table:
SELECT ... INTO Syntax
The SELECT ... INTO form of SELECT enables a
query result to be stored in variables or written to a file:
SELECT ... INTO var_list selects column values and stores them into
variables.
SELECT ... INTO OUTFILE writes the selected rows to a file. Column and
line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILE writes a single row to a file without any
formatting.
I do remember having similar troubles while working with SQL myself. One cause of error I found was the use of citation marks... try removing the citation marks like this:
SELECT * INTO zbackup_oc_t_city FROM oc_t_city;
I'm not sure this fixes your problem (but I can't see anything else wrong with your query). I hope it does though. :)

delete query for mysql using c

can anyone tell me the correct query to delete values from mysql db table,in my case the table name and id are accepted from the user and the row is deleted based on id.This is my query
sprintf(Query,"DELETE FROM ('%s') where id = (%d)",tb1,idt1) ;
/*table name is in form of string and id is int */
mysql_query(conn,Query);
You should remove parentheses around the table name:
sprintf(Query,"DELETE FROM '%s' where id = (%d)",tb1,idt1) ;
MySQL considers queries like this syntax errors:
delete from (mytable) where id=2;
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 '(mytable) where id=2' at line 1
(I'll assume that you know everything about SQL injection attacks, and that neither tb1 nor idt1 are constructed from user input in any shape or form).