I have been attempting to reorder a table in a database based on category and order id for a menu structure.
This is what I have roughly as a table structure
menuid menuname order categoryid -------------------------------------- 1 firstitem 1 1 2 seconditem 2 1 3 thirditem 1 2 4 fourthitem 2 2
I have tried this syntax:
UPDATE yourtable SET id=IF(id=2, 3, 2) where id in(2,3)
like this:
UPDATE menu
SET order = IF(order = `3`, `2`, `3`)
WHERE order = `3`
I am getting this 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 'order = IF(order = `3`, `2`, `3`) WHERE order = `3`' at line 2
Solution code:
UPDATE menu
SET `order` = case when `order` = 3 then 2 else 3 end
WHERE `order` = 3 AND section = 2 OR `order` = 2 AND section = 2
try
By the way
orderis a reserved word in MySQL. You have to escape it with backticks