是否可以在一个查询中更新同一列中的2个字段(mysql)

时间:2013-08-06 12:07:51

标签: mysql sql sql-update

我有一个名为'系统'的表有2列'变量'和'' 以下数据:

  • variable ='username'=> value ='myuser'
  • variable ='password'=> value ='mypass'

我想在1个查询中更新这两个字段。

2个查询是:

UPDATE System SET Value = 'myuser' WHERE Variable = 'Username'
UPDATE System SET Value = 'mypass' WHERE Variable = 'Password'

5 个答案:

答案 0 :(得分:4)

UPDATE System
SET Value = (CASE
    WHEN Variable = 'Username' THEN 'myuser'
    WHEN Variable = 'Password' THEN 'mypass'
  END)
 WHERE Variable = 'Username' or Variable = 'Password'

答案 1 :(得分:2)

是的,使用他的案例陈述:

UPDATE System
    SET Value = (case when Variable = 'UserName' then 'myuser'
                      when Variable = 'Password' then 'mypass'
                      else Value
                 end)
    WHERE Variable in ('Username', 'Password');

答案 2 :(得分:0)

UPDATE System 
SET Value = case when Variable = 'Username' then 'myuser' 
                 when Variable = 'Password' then 'mypass' 
            end
WHERE Variable in ('Username', 'Password')

答案 3 :(得分:0)

UPDATE System SET
 Value = (case Variable when 'Username' then 'myuser' when 'Password' then 'mypass' end)
WHERE Variable  = 'Username' or Variable = 'Password'

答案 4 :(得分:0)

UPDATE System SET `Value` = IF(`Variable`='Username', 'myuser', 'mypassw') 
WHERE `Value` IN ('Username', 'Password');