MYSQL, multiple insert and ON DUPLICATE UPDATE

别等时光非礼了梦想. 提交于 2019-12-02 17:50:40

问题


I'm really blocked with multiple insert values and if any one exist, do custom update. Here is my query that not work :

INSERT INTO `table` (f1,f2,status) VALUES 
(1,5,'on') ON DUPLICATE KEY UPDATE status='off', 
(3,2,'on') ON DUPLICATE KEY UPDATE status='off', 
(15,20,'on') ON DUPLICATE KEY UPDATE status='off';

There is any solution that can do this query?

Thanks for everyone


回答1:


You can only have one ON DUPLICATE KEY per INSERT:

INSERT INTO `table`(f1, f2, status)
    SELECT 1 ,5, 'on' UNION ALL 
    SELECT 3, 2, 'on' UNION ALL
    SELECT 15, 20, 'on'
    ON DUPLICATE KEY UPDATE status = 'off';

(Of course, you can also do this with VALUES; I just prefer SELECT because it is more general.)



来源:https://stackoverflow.com/questions/31048790/mysql-multiple-insert-and-on-duplicate-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!