假设我有查询:
INSERT INTO TABLE (column1, column2, column3, column4) VALUES
(val11, val21, val31, val41),
(val12, val22, val32, val 42)
....
And then I put in a whole bunch of more values
ON DUPLICATE KEY UPDATE (some update clause)
是否可以修改此查询,以便对于某些行(假设它们是重复的键),我设置它以使column3更新为我设置的值,而第4列保持不变,而对于某些行,我将column4更新为某些值,而column3保持原样...即。是否有可能有选择地更新这一个查询中的不同列,将其余列保留原样而不将其拆分为多个查询?
答案 0 :(得分:1)
看起来MySQL支持在DUPLCIATE KEY UPDATE子句中执行条件逻辑
http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql
IF允许您提供“if-else”参数。所以我认为你可以做这样的事情
INSERT INTO table (column1, column2, column3, column4)
VALUES (val11, val21, val31, val41), (val12, val22, val32, val42)
ON DUPLICATE KEY UPDATE
column3 = IF(<some_condition>, <new_value>, <cur_value>),
column4 = IF(<some_condition>, <new_value>, <cur_value>);