Mysql syntax error during an update but code look fine - mysql

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

Related

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.

Simple SELECT SQL statement not working

SELECT user FROM members WHERE admin=1;
SELECT user FROM members WHERE id=1;
The first query does not work because there's an error in my syntax. The second works but they are virtually identical. Both 'admin' and 'id' are INT(11), but 'id' is the primary key and set to auto-increment. What's wrong here?
The error I receive is:
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 'admin=1' at line 1
When you use keywords or reserved words don't forget to add quotes or brackets.
SELECT [user] FROM members WHERE [admin]=1;
SELECT [user] FROM members WHERE id=1;
I apologize for the confusion everyone, my fault. I did this and it works.
SELECT user FROM members WHERE members.group=2;

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;

sql update query with space separated column name

Update Student Set First Name='adwd' Where StudentID=123;
ERROR 1064 (42000): You have an error in your SQL syntax;
This query is not working in mysql , because the column name in my table is space separated. if i do the query on another column which is not comma separated it will work.
However ,i want to keep the first name as is. I tried using [] and `` and "" and '' none of them have worked. Is this just impossible to do and i have to rename my column names ?
I hope some1 can provide a good solution
thx community :)
try this
Update Student Set `First Name`='adwd' Where StudentID=123;
If you are using MySQL, the correct syntax is:
Update Student
Set `First Name` = 'adwd'
Where StudentID = 123;
If this doesn't work, then something else is wrong with the query apart from the column name. Note that backticks are used for escaping the column name in MySQL.

MySQL foreign key problem

I'm getting that error running on MySQL 5.5.8
Mysql2::Error: 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 'WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?
I suspect the problem is there is no FROM clause in your select statement.
Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?