1

我有一个名为“系统”的表有 2 列“变量”和“”,其中包含以下数据:

  • 变量 = '用户名' => 值 = 'myuser'
  • 变量 = '密码' => 值 = 'mypass'

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

2个查询是:

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

5 回答 5

4
UPDATE System
SET Value = (CASE
    WHEN Variable = 'Username' THEN 'myuser'
    WHEN Variable = 'Password' THEN 'mypass'
  END)
 WHERE Variable = 'Username' or Variable = 'Password'
于 2013-08-06T12:11:26.080 回答
2

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

UPDATE System
    SET Value = (case when Variable = 'UserName' then 'myuser'
                      when Variable = 'Password' then 'mypass'
                      else Value
                 end)
    WHERE Variable in ('Username', 'Password');
于 2013-08-06T12:12:41.117 回答
0
UPDATE System SET `Value` = IF(`Variable`='Username', 'myuser', 'mypassw') 
WHERE `Value` IN ('Username', 'Password');
于 2013-08-06T12:31:10.337 回答
0
UPDATE System 
SET Value = case when Variable = 'Username' then 'myuser' 
                 when Variable = 'Password' then 'mypass' 
            end
WHERE Variable in ('Username', 'Password')
于 2013-08-06T12:12:12.733 回答
0
UPDATE System SET
 Value = (case Variable when 'Username' then 'myuser' when 'Password' then 'mypass' end)
WHERE Variable  = 'Username' or Variable = 'Password'
于 2013-08-06T12:13:06.667 回答