【发布时间】:2020-06-19 16:05:55
【问题描述】:
我有一个 mysql 表来记录这样的两个人的比赛:
- gameid
- id1 // id of player 1
- id2 // id of player 2
- score1 // score of player 1
- score2 // score of player 2
- state // state of the games is initially 0, then the score updates are made and in order to prevent further updates the state must be updated to 1
我需要检查记录并根据分数更新另一个表“用户”。前任: 如果 score1 > score2 我需要更新 3 件事:
1- the state of the game // from 0 to 1
2- in table "users" add 1 point to the column score for the user with userid = id1
2- in table "users" subtract 1 point from the column score for the user with userid = id2
到目前为止,我可以更新 1 和 2,但我需要在一个命令中完成所有 3 个更新:
UPDATE dbo.games AS GA , dbo.users AS US
SET GA.state = 1, US.score = US.score + 1
WHERE US.id = GA.id1 AND GA.state = 0 and GA.score1 > GA.score2
我可以将 +1 和 -1 命令分开,它可以正常工作。但是当命令运行时,两个用户的分数都应该更新。有人可以帮忙吗?
【问题讨论】:
-
你能写一个返回等效结果的SELECT吗?
-
并确认您的 RDBMS
标签: mysql sql join sql-update