我目前正在为一个联盟剧本工作。 这些是我的mysql表:https://pastebin.com/raw/iuNbzGTS 这是我当前的SQL查询:
SELECT
name AS Team, Sum(P) AS P,Sum(W) AS W,Sum(L) AS L,SUM(Pts) AS Pts
FROM(
SELECT
teamid1 Team,
1 P,
IF(team1score > team2score,1,0) W,
IF(team1score < team2score,1,0) L,
CASE WHEN team1score > team2score THEN 1 WHEN team1score = team2score THEN 1 ELSE 0 END PTS
FROM games
UNION ALL
SELECT
teamid2,
1,
IF(team1score < team2score,1,0),
IF(team1score > team2score,1,0),
CASE WHEN team1score < team2score THEN 1 WHEN team1score = team2score THEN 1 ELSE 0 END
FROM games
) as tot
JOIN teams t ON tot.Team=t.id
GROUP BY Team
ORDER BY SUM(Pts) DESC ;
问题在于它只显示已经进行了比赛的球队。 SQL查询不返回其他团队。我想回到桌子底部的球队,但还没有打过一场比赛。 我怎么能这样做?
谢谢 最诚挚的问候
答案 0 :(得分:1)
使用left join
:
FROM teams t
LEFT JOIN
(
... your subquery here ...
) as tot
ON tot.Team = t.id