我有3张桌子:
我有3个单独运行的查询:
SELECT T.name, count(TS.victory) as Wins
FROM Teams T
JOIN Seasons S ON T.id_season = S.id_season AND S.name = '2015'
LEFT JOIN Teams_Stats TS ON TS.id_team = T.id_team AND TS.victory = 1
GROUP BY T.name
ORDER BY T.name
SELECT T.name, count(TS.defeat) as Losses
FROM Teams T
JOIN Seasons S ON T.id_season = S.id_season AND S.name = '2015'
LEFT JOIN Teams_Stats TS ON TS.id_team = T.id_team AND TS.defeat = 1
GROUP BY T.name
ORDER BY T.name
SELECT T.name, count(TS.victory) as Draws
FROM Teams T
JOIN Seasons S ON T.id_season = S.id_season AND S.name = '2015'
LEFT JOIN Teams_Stats TS ON TS.id_team = T.id_team AND TS.draw = 1
GROUP BY T.name
ORDER BY T.name
我想知道如何才能获得相同的结果,但只能在一个查询中。我无法得到它?也许有人可以抛光......
我很感激,
感谢。
答案 0 :(得分:1)
您可以使用条件聚合:
SELECT T.name,
sum(TS.victory=1) as Wins,
sum(TS.defeat=1) as Losses,
sum(TS.draw =1) as Draws
FROM Teams T
JOIN Seasons S ON T.id_season = S.id_season AND S.name = '2015'
LEFT JOIN Teams_Stats TS ON TS.id_team = T.id_team
GROUP BY T.name
ORDER BY T.name