我需要你的幫助:的MySQL如何使用變量的值的查詢在自己的子查詢
我有一組爲用戶設置,遊戲和結果的關係表中,我試圖來計算每個用戶贏取百分比。
由於很多瀏覽我發現了以下方法:
SELECT Winner, (COUNT(Winner) * games.TotalPlayed) AS PercentWon
FROM Results
JOIN (
SELECT 100/COUNT(*) AS TotalPlayed
FROM games
where Player_1 = 'John' OR Player_2 = 'John'
) AS games
where Winner == 'John'
GROUP BY Winner
ORDER BY PercentWon DESC;
這完全適用於獲得約翰斯%,但我希望它掃描整個表的結果和打印值的每一個人。不過,我不允許使用:
SELECT Winner, (COUNT(Winner) * games.TotalPlayed) AS PercentWon
FROM Results
JOIN (
SELECT 100/COUNT(*) AS TotalPlayed
FROM games
where Player_1 = Winner OR Player_2 = Winner
) AS games
where Winner != 'Draw'
GROUP BY Winner
ORDER BY PercentWon DESC;
爲獲獎者沒有定義。 Winner在執行時如何獲得傳遞給子查詢的值?
請張貼結構(結果表中的重要列) –
結果表最多 – user2240912
有一個球員表?如果是這樣,你可以分享它的模式嗎? – DiscoInfiltrator