SELECT f.user_id, f.item_id, f.status, f.shipped,
CASE
WHEN f.status = 0 THEN UPDATE f SET f.status = 1 WHERE f.user_id = 1 AND f.item_id = 1
WHEN f.status = 1 THEN UPDATE f SET f.status = 0 WHERE f.user_id = 1 AND f.item_id = 1
ELSE 'ERROR'
END CASE
FROM business AS f;
Hi, I want to update the status of an item based on its value ( 0 or 1). I still got syntax error. Am I using the right conditional statements or should I use IF()? If so, how can I put another query inside the IF()?
Thank you!
2
Answers
This is very simple SQL query:
If you want to update the actual data in the table, then use an
UPDATE
statement, not aSELECT
statement:Running this will update your
business
table for the record(s) whereuser_id = 1
anditem_id=1
switching the values ofstatus
unless the current status value isn’t1
or0
, in which case it will setstatus
to-1
. Note that the string'error'
can’t be used a value unless this column is avarchar
or similar string-type column.