key
is a reserved keyword. enclose it in backticks as
UPDATE PREFERENCES SET value='somethingElse' WHERE userid=12 AND `key`='abc'
Question
I have a table 'PREFERENCES' having columns userid (integer), key (varchar(255)) and value (longtext). Columns (userid, key) is primary key. I have some data already in table.
For example:
userid | key | value
12 | abc | testvalue12abc
12 | pqr | testvalue12pqr
13 | abc | testvalue13abc
14 | abc | testvalue14abc
14 | pqr | testvalue14pqr
14 | xyz | testvalue14xyz
When I run update query,
UPDATE PREFERENCES
SET value='somethingElse'
WHERE userid=12 AND key='abc';
I get the syntax 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 'userid=12 AND key ='abc'' at line 1`.
I tried different combinations in where clause; It works fine when there is no varchar column in where clause. But it fails with syntax error when varchar column is used in where clause.
I am using mysql, server 5.1.49.
Solution
key
is a reserved keyword. enclose it in backticks as
UPDATE PREFERENCES SET value='somethingElse' WHERE userid=12 AND `key`='abc'