MySQL并让每个玩家获得'他们最后一场比赛的状态

时间:2017-08-01 21:06:51

标签: mysql sql database

我有一个MySQL数据库。有1场比赛和1场比赛的比赛。换句话说,每场比赛2名球员。每位球员每场比赛只能进行一次。玩家也可能没有参加所有比赛。

我还希望找到每个玩家所玩的最后一场比赛,并将该比赛的玩家状态设为end_status。这不起作用。

换句话说,如果玩家在最后一场比赛中是1号球员,我希望将end_status_player1.status作为end_status。但是如果玩家在最后一场比赛中是2号球员,那么end_status_player2.status应该被用作end_status

SELECT (end_status_player1.status, end_status_player2.status) AS end_status
FROM players
INNER JOIN teams ON teams.id = players.team_id /* OK */

/* if this is player's last match and he is player 1, this should be used */

LEFT JOIN matches AS player1 ON player1.chart_id = players.comp_id
                           AND player1.player1_id = players.id
LEFT JOIN status AS end_status_player1 ON end_status_player1.id = player1.user_status_id

/* if this is player's last match and he is player 2, this should be used */

LEFT JOIN matches AS player2 ON player2.chart_id = players.comp_id
                           AND player2.player2_id = players.id
LEFT JOIN status AS end_status_player2 ON end_status_player2.id = player2.user_status_id

WHERE players.comp_id = 107
GROUP BY teams.id

0 个答案:

没有答案