2

以下の点についてお役に立てれば幸いです

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エラーが発生するようになりました。

ハンディキャップは、私のplayersテーブルに追加された新しいフィールドだったので、元のテーブルにSQLを少し追加するだけでよいと思いました。

何かご意見は?

ありがとうございます

4

1 に答える 1

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;
于 2013-01-15T14:29:38.443 に答える