SELECT COUNT(m.winner = p.userName) AS wins, YEAR(m.datePlayed) AS year, DATE_FORMAT(m.datePlayed, '%M') AS month, COUNT(*) as games FROM allMatches AS m LEFT JOIN playerUsers AS p ON m.season = p.season AND (m.player1 = p.userName OR m.player2 = p.userName) WHERE p.realName = '$realName' AND m.winner != '' AND m.canceled != '1' GROUP BY YEAR(m.datePlayed), MONTH(m.datePlayed)";
我们要加入 2 个表:allMatches 和 playerUsers。PlayerUser 的用户名与给定赛季的 allMatches 表中的 player1 或 player2(也可能与获胜者)匹配。
我想计算玩的游戏数量(他们参加的任何比赛)和他们赢得的比赛数量(他们在 playerUsers 上的用户名与 allMatches 上的获胜者匹配)。上面的 SQL 语句以相同的数字返回胜利和游戏。有没有办法在同一个 SQL 语句中统计胜数和游戏数?