如何在一个MySQL查询中进行2次更新

时间:2014-02-12 19:32:00

标签: mysql

在MySQL查询中

如何在一个查询中进行2次更新,查询如下:

UPDATE `stats` SET `coin` = coin + 500 WHERE `player` = 'userone'
UPDATE `stats` SET `coin` = coin - 500 WHERE `player` = 'usertwo'

2 个答案:

答案 0 :(得分:1)

您可以使用casein

UPDATE `stats`
    SET `coin` = coin + (case when player = 'userone' then 500 else -500 end)
    WHERE `player` in ( 'userone', 'usertwo');

答案 1 :(得分:1)

UPDATE stats
SET coin = CASE player
            WHEN 'userone' THEN coin + 500
            WHEN 'usertwo' THEN coin - 500
           END
WHERE player IN ('userone', 'usertwo')