如果mysql中存在不起作用

时间:2013-07-17 13:23:56

标签: mysql if-statement exists

    IF EXISTS (SELECT * FROM pages WHERE pages.id = 21)
UPDATE `pages` SET `content`='Updated' WHERE (`id`='21')
ELSE
INSERT INTO `pages` (`subject_id`) VALUES ('102')

不工作 有什么问题

1 个答案:

答案 0 :(得分:1)

试试这个

 INSERT INTO `pages` (`subject_id`) VALUES ('102')
 ON DUPLICATE KEY UPDATE `content`='Updated' 
 WHERE `id`='21'