MySQL Update error while updating table - mysql

I have table in MySQL with years as column names like 1960, 1961, 1962... etc. Records are being inserted successfully. When I try to update table with query
UPDATE table1 SET 1960=0.0 WHERE id = 'abc'
it gives:
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 '1960=0.0 WHERE id='abc' at line 1
Is it due to columns names as numbers or something else is wrong?

try this:
UPDATE table1 SET `1960`='0.0' WHERE id = 'abc'
... added backticks to column name and single quotes around value (not really required for the value, but I always do it)

escape your column name with backticks
UPDATE table1 SET `1960` = 0.0 WHERE id = 'abc'
That has to be done if your column name is a reserved keyword in MySQL or a number.

You'll have to escape your column names by using the backtick character. The following manual page is somewhat dense but informative
Try...
UPDATE table1 SET `1960`=0.0 WHERE id = 'abc'

Related

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 syntax error for update rows

update amazon-crawler set `flag_images`= '0' where `id`='966'
i get #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 '-crawler set flag_images= '0' where id='966'' at line 1
why syntax error?
amazon-crawler is the table
flag images and id are columns
My guess is that the hyphen in the table name is causing problems, because it is an arithmetic operator. Try also escaping the table name:
UPDATE `amazon-crawler` SET `flag_images`= '0' WHERE `id` = '966';
Note that you should try to avoid using backticks in your query, unless absolutely needed. Using backticks means that any name would potentially work, even one which happens to be a MySQL reserved keyword. Also, I'm guessing that flag_images and id are numeric columns, in which case you should be comparing them against numbers, not strings. So I would write your update as this:
UPDATE `amazon-crawler` SET flag_images= 0 WHERE id = 966;
Here, only the table name has to appear in backticks.

Update longtext field in mysql

Im trying to update a longtext type field called 'comment' using a simple sql query in mysql client like this :
Update mytable set comment='Test' where id = 1;
But i'm getting this error
ERROR 1064 (42000): 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 'comment='Test' where id = 1' at line 1
Am i missing something ?, thanks in advance.
comment is a reserved word, if you want to have a table/field with that name, you have to quote it (or use the table.fieldname syntax, in case of a field). default in mysql is the backtick for that, so:
update mytable set `comment`='Test' where id = 1;
Found it, it gets solved with this:
update mytable as a set a.comment='Test' where id = 1;

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).

Mysql syntax error during an update but code look fine

I have a strange issue with a Mysql Update and I'm not sure what is causing it. I suspect something is wrong with the table itself but the field causing the error appears to be defined the same as other fields in the table. I can recreate the error in phpMyAdmin on the SQL tab and also in php code. I am completely stumped.
The fields in the table are defined as follows:
bnumber is INT length=11
bname is VARCHAR length=60 Collation=latin1_swedish_ci
twittername is VARCHAR length=15 Collation=latin1_swedish_ci
desc is VARCHAR length=60 Collation=latin1_swedish_ci
This update statement works:
update tbl1 set bname='myName', twittername='myTweet' where bnumber=1;
this one gives me the error:
update tbl1 set bname='myName', twittername='myTweet', desc='test' where bnumber=1;
the error I get is:
#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='Main' where bnumber=1' at line 1.
I don't seem to have any issues selecting from the table or inserting to the table. Only update is giving me the error.
I appreciate any help.
Thanks!
desc is a reserved word in MySQL. You must quote it in backticks.
desc is a keyword. Escape it with backticks.
update tbl1
set bname='myName',
twittername='myTweet',
`desc`='test'
where bnumber=1;
desc is a reserved word in mysql
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
Updated your sql as below and it will work fine;
update tbl1 set bname='myName', twittername='myTweet', `desc`='test'
where bnumber=1;
you cant use desc as a field name.
change fieldname to desc1 and try.
desc is for oder by name desc