如何在MySQL中进行条件更新/删除?

时间:2015-03-04 15:16:31

标签: mysql

我需要一些能够查找某些记录然后决定是删除还是更新的内容,具体取决于第一个查询的结果。例如:

IF (SELECT COUNT(*) FROM Items WHERE itemId = 101) = 0 THEN
  DELETE FROM itemPrice WHERE itemId = 101;
ELSE
  UPDATE itemPrice SET price = 0 WHERE itemId = 101;
END

我不确定如何做到这一点,是否可以在MySQL?

1 个答案:

答案 0 :(得分:0)

您可以使用2个不同的sql命令:
DELETE FROM itemPrice WHERE itemId not in (select itemid FROM Items)); UPDATE itemPrice SET price = 0 WHERE itemId in (select itemid FROM Items));