SQL - 在一个查询中更新多条记录

2021-11-20 00:00:00 mysql record

我有表格 - config.架构:config_name |配置值

I have table - config. Schema: config_name | config_value

我想在一个查询中更新多条记录.我是这样尝试的:

And I would like to update multiple records in one query. I try like that:

UPDATE config 
SET t1.config_value = 'value'
  , t2.config_value = 'value2' 
WHERE t1.config_name = 'name1' 
  AND t2.config_name = 'name2';

但是那个查询是错误的:(

but that query is wrong :(

你能帮我吗?

推荐答案

尝试任一多表更新语法

UPDATE config t1 JOIN config t2
    ON t1.config_name = 'name1' AND t2.config_name = 'name2'
   SET t1.config_value = 'value',
       t2.config_value = 'value2';

这是SQLFiddle 演示

Here is SQLFiddle demo

或有条件更新

UPDATE config
   SET config_value = CASE config_name 
                      WHEN 'name1' THEN 'value' 
                      WHEN 'name2' THEN 'value2' 
                      ELSE config_value
                      END
 WHERE config_name IN('name1', 'name2');

这是SQLFiddle 演示

Here is SQLFiddle demo

相关文章