我使用以下查询来查看结果表并显示各个玩家的比赛的胜利百分比。
SELECT
T.member
, count(*) Wins
, total.gamesPlayed
, count(*)/total.gamesPlayed as winPercentage
FROM
(
SELECT
player2_name as member
from results
WHERE
player1_id = 2
AND player1_result = 'W'
AND community_id = 16
UNION ALL
SELECT
player1_name
FROM results
WHERE player2_id = 2
AND player2_result = 'W'
AND community_id = 16
) AS T
, (
select
u.member
, sum(u.gamesPlayed) as gamesPlayed
from
(
select
player2_name as member
, count(*) gamesPlayed
from results
where
player1_id = 2
AND community_id = 16
group by player2_name
union
select
player1_name as member
, count(*) gamesPlayed
from results
where
player2_id = 2
AND community_id = 16
group by player2_name
) as u /* union of two */
group by member
) as total
where
T.member = total.member
group by T.member
ORDER BY Wins DESC
正确的输出应该是:
+---------+-----------+---------------+---------+
| member | Wins | Games Played | Win % |
+---------+-----------+---------------+---------+
| Player 1| 3 | 4 | 0.75 |
| Player 2| 2 | 2 | 1.00 |
+---------+-----------+---------------+---------+
但我现在正在接受:
+---------+-----------+---------------+---------+
| member | Wins | Games Played | Win % |
+---------+-----------+---------------+---------+
| Player 1| 3 | 1 | 3.00 |
| Player 2| 2 | 5 | 0.40 |
+---------+-----------+---------------+---------+
如您所见,故障似乎出现在Games Played列中,提取的数据不正确。如何更正我的查询以获得我追求的结果?