2013-01-15 94 views
2

後,我將不勝感激一些幫助,下面SQL errror增加額外的選擇

SELECT distinct playerid as le_player, 
    (select sum(score) from playerresults where playerid = le_player) as wins, 
    (select handicap from players where playerid = le_player) as handicap, playername, 
    (select count(playerid)*3 from playerresults where playerid = le_player)as totalgames, 
    (select count(playerid)*3 from playerresults where playerid = le_player) - (select sum(score) from playerresults where playerid = le_player)as lost, 
    round((select sum(score) from playerresults where playerid = le_player)/(select count(playerid)*3 from playerresults where playerid = le_player) * 100,2) as percent, 
    teams.team_name 
FROM playerresults 
    INNER JOIN teams on (select players.team_id from players where players.id = playerid) = teams.id 
WHERE playerresults.season = 2012 AND playerresults.league = 4 
ORDER BY wins desc,totalgames asc 

這是工作,直到我說下面一行:

(select handicap from players where playerid = le_player) as handicap 

現在,這產生了Subquery returns more than 1 row錯誤。

障礙是一個新的領域添加到我的球員表,所以我想我可以只添加這一點的SQL到我的原始。

有什麼想法?

感謝 DG

+4

@juergend你認爲你可以幫助他們嗎? –

回答

5

你可能要考慮使用與表,而不是所有的相關子查詢的聯接:

SELECT playerid as le_player, 
    sum(pr.score) as wins, 
    p.handicap, 
    pr.playername, 
    count(pr.playerid)*3 as totalgames, 
    count(pr.playerid)*3 - sum(pr.score) as lost, 
    round(sum(pr.score)/(count(pr.playerid)*3) * 100,2) as percent, 
    t.team_name 
from playerresults pr 
left join players p 
    on pr.playerid = p.id 
left join teams t 
    on p.team_id = teams.id 
where pr.season = 2012 
    AND pr.league = 4 
group by pr.playerid 
ORDER BY wins desc, totalgames asc; 

甚至是這樣的:

SELECT pr.playerid as le_player, 
    Score as wins, 
    p.handicap, 
    pr.playername, 
    CountPlayerId*3 as totalgames, 
    CountPlayerId*3 - Score as lost, 
    round(Score/(CountPlayerId*3) * 100,2) as percent, 
    t.team_name 
from 
(
    select playerid, 
     sum(pr.score) Score, 
     count(pr.playerid) CountPlayerId 
    from playerresults 
    WHERE season = 2012 
     AND league = 4 
    group by playerid 
) pr 
left join players p 
    on pr.playerid = p.id 
left join teams t 
    on p.team_id = teams.id 
where pr.season = 2012 
    AND pr.league = 4 
ORDER BY wins desc, totalgames asc; 
+0

哇,這是做的伎倆。非常感謝。 – user1980578

+1

@ user1980578 - 嘗試[**接受**](http://meta.stackexchange.com/questions/16721/how-does-accept-rate-work)答案,通過檢查它的左邊的標記。這是怎麼東西在這裏工作。 –

+0

明白了,謝謝。 – user1980578