MySQL—在一个查询中更新多个不同值的行。

时间:2021-03-22 23:09:53

I am trying to understand how to UPDATE multiple rows with different values and I just don't get it. The solution is everywhere but to me it looks difficult to understand.

我正在尝试理解如何用不同的值更新多个行,但我就是不明白。解决办法到处都是,但对我来说,似乎很难理解。

For instance, three updates into 1 query:

例如,三个更新为一个查询:

UPDATE table_users
SET cod_user = '622057'
    , date = '12082014'
WHERE user_rol = 'student'
    AND cod_office = '123456'; 

UPDATE table_users
SET cod_user = '2913659'
    , date = '12082014'
WHERE user_rol = 'assistant'
    AND cod_office = '123456'; 

UPDATE table_users
SET cod_user = '6160230'
    , date = '12082014'
WHERE user_rol = 'admin'
    AND cod_office = '123456'; 

I read an example, but I really don't understand how to make the query. i.e:

我读了一个例子,但我真的不知道如何进行查询。即:

UPDATE table_to_update
SET cod_user= IF(cod_office = '123456','622057','2913659','6160230')
    ,date = IF(cod_office = '123456','12082014')
WHERE ?? IN (??) ;

I'm not entirely clear how to do the query if there are multiple condition in the WHERE and in the IF condition..any ideas?

如果WHERE和if条件中有多个条件,我不完全清楚如何执行查询。什么好主意吗?

4 个解决方案

#1


98  

You can do it this way:

你可以这样做:

UPDATE table_users
    SET cod_user = (case when user_role = 'student' then '622057'
                         when user_role = 'assistant' then '2913659'
                         when user_role = 'admin' then '6160230'
                    end),
        date = '12082014'
    WHERE user_role in ('student', 'assistant', 'admin') AND
          cod_office = '17389551';

I don't understand your date format. Dates should be stored in the database using native date and time types.

我不明白你的日期格式。日期应该使用本机日期和时间类型存储在数据库中。

#2


51  

MySQL allows a more readable way to combine multiple updates into a single query. This seems to better fit the scenario you describe, is much easier to read, and avoids those difficult-to-untangle multiple conditions.

MySQL允许将多个更新合并到一个查询中,这是一种更可读的方法。这似乎更适合您描述的场景,更容易阅读,并且避免了那些难以理清的多种情况。

INSERT INTO table_users (cod_user, date, user_rol, cod_office)
VALUES
('622057', '12082014', 'student', '123456'),
('2913659', '12082014', 'assistant','123456'),
('6160230', '12082014', 'admin', '123456')
ON DUPLICATE KEY UPDATE
 cod_user=VALUES(cod_user), date=VALUES(date)

This assumes that the user_rol, cod_office combination is a primary key. If only one of these is the PK, then add the other field to the UPDATE list. If neither of them is a primary key (that seems unlikely) then this approach will always create new records - probably not what is wanted.

这假定user_rol、cod_office组合是主键。如果其中只有一个是PK,那么将其他字段添加到更新列表中。如果它们都不是主键(这似乎不太可能),那么这种方法将总是创建新的记录——可能不是需要的。

However, this approach makes prepared statements easier to build and more concise.

然而,这种方法使准备好的语句更容易构建,也更简洁。

#3


6  

You can use a CASE statement to handle multiple if/then scenarios:

您可以使用CASE语句来处理多个if/then场景:

UPDATE table_to_update 
SET  cod_user= CASE WHEN user_rol = 'student' THEN '622057'
                   WHEN user_rol = 'assistant' THEN '2913659'
                   WHEN user_rol = 'admin' THEN '6160230'
               END
    ,date = '12082014'
WHERE user_rol IN ('student','assistant','admin')
  AND cod_office = '17389551';

#4


2  

update table_name
set cod_user = 
    CASE 
    WHEN user_rol = 'student' THEN '622057'
    WHEN user_rol = 'assistant' THEN '2913659'
    WHEN user_rol = 'admin' THEN '6160230'?
    END,date = '12082014'

WHERE user_rol IN ('student','assistant','admin')
AND cod_office = '17389551';

#1


98  

You can do it this way:

你可以这样做:

UPDATE table_users
    SET cod_user = (case when user_role = 'student' then '622057'
                         when user_role = 'assistant' then '2913659'
                         when user_role = 'admin' then '6160230'
                    end),
        date = '12082014'
    WHERE user_role in ('student', 'assistant', 'admin') AND
          cod_office = '17389551';

I don't understand your date format. Dates should be stored in the database using native date and time types.

我不明白你的日期格式。日期应该使用本机日期和时间类型存储在数据库中。

#2


51  

MySQL allows a more readable way to combine multiple updates into a single query. This seems to better fit the scenario you describe, is much easier to read, and avoids those difficult-to-untangle multiple conditions.

MySQL允许将多个更新合并到一个查询中,这是一种更可读的方法。这似乎更适合您描述的场景,更容易阅读,并且避免了那些难以理清的多种情况。

INSERT INTO table_users (cod_user, date, user_rol, cod_office)
VALUES
('622057', '12082014', 'student', '123456'),
('2913659', '12082014', 'assistant','123456'),
('6160230', '12082014', 'admin', '123456')
ON DUPLICATE KEY UPDATE
 cod_user=VALUES(cod_user), date=VALUES(date)

This assumes that the user_rol, cod_office combination is a primary key. If only one of these is the PK, then add the other field to the UPDATE list. If neither of them is a primary key (that seems unlikely) then this approach will always create new records - probably not what is wanted.

这假定user_rol、cod_office组合是主键。如果其中只有一个是PK,那么将其他字段添加到更新列表中。如果它们都不是主键(这似乎不太可能),那么这种方法将总是创建新的记录——可能不是需要的。

However, this approach makes prepared statements easier to build and more concise.

然而,这种方法使准备好的语句更容易构建,也更简洁。

#3


6  

You can use a CASE statement to handle multiple if/then scenarios:

您可以使用CASE语句来处理多个if/then场景:

UPDATE table_to_update 
SET  cod_user= CASE WHEN user_rol = 'student' THEN '622057'
                   WHEN user_rol = 'assistant' THEN '2913659'
                   WHEN user_rol = 'admin' THEN '6160230'
               END
    ,date = '12082014'
WHERE user_rol IN ('student','assistant','admin')
  AND cod_office = '17389551';

#4


2  

update table_name
set cod_user = 
    CASE 
    WHEN user_rol = 'student' THEN '622057'
    WHEN user_rol = 'assistant' THEN '2913659'
    WHEN user_rol = 'admin' THEN '6160230'?
    END,date = '12082014'

WHERE user_rol IN ('student','assistant','admin')
AND cod_office = '17389551';