0

我有一张看起来像这样的桌子

id |game_id| player_id| 优胜者
----------------------------
 1 | 1 | 1 |    
 2 | 1 | 2 |
 3 | 1 | 3 |
 4 | 1 | 4 |

我有一个游戏 ID 和获胜者 ID,我想将获胜者更新为 1,将失败者更新为 0;

UPDATE tournament SET winner = '1' WHERE game_id= 1 AND  player_id = 1

然后

UPDATE tournament SET winner = '0' WHERE game_id= 1 AND  player_id != 1

输出

id |game_id| player_id| 优胜者
----------------------------
 1 | 1 | 1 | 1
 2 | 1 | 2 | 0
 3 | 1 | 3 | 0
 4 | 1 | 4 | 0

谁能告诉我如何使用单个查询来完成

4

2 回答 2

2
UPDATE tournament SET winner = IF(player_id = 1, '1', '0') WHERE game_id= 1
于 2013-03-18T21:00:02.273 回答
0

询问

SQLFIDDLE示例

UPDATE tournament 
SET winner = CASE WHEN player_id = 1
                  THEN '1'
                  ELSE '0'END
WHERE game_id= 1
于 2013-03-18T21:47:43.737 回答