如果第一列不为null,我想更新第二列值,否则更新最合适的列。下面是我查询的查询,但此查询给出了语法错误。以下是我的查询
UPDATE messages
SET CASE WHEN delete_one IS NULL THEN delete_one = 9600 ELSE delete_two = 9600 END
WHERE
( message_to = 9600 AND message_from = 8625 )
OR
( message_to = 8625 AND message_from = 9600);
以下是运行此查询后我收到的语法错误
Error Code: 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 'case when delete_one IS NULL THEN delete_one = 9600 ' at line 3
请帮助解决此问题。
提前致谢
答案 0 :(得分:2)
CASE Expression必须位于SET的右侧:
UPDATE messages
SET delete_two = CASE WHEN delete_one IS NOT NULL THEN 9600 ELSE delete_two END,
delete_one = COALESCE(delete_one,9600)
WHERE
( message_to = 9600 AND message_from = 8625 )
OR
( message_to = 8625 AND message_from = 9600);
答案 1 :(得分:0)
尝试分配CASE
表达式:
UPDATE messages
SET delete_one = COALESCE(delete_one, 9600),
delete_two = CASE WHEN delete_one IS NULL THEN delete_two ELSE 9600 END
WHERE
(message_to = 9600 AND message_from = 8625 ) OR
(message_to = 8625 AND message_from = 9600);