我有问题。 (SQL begginer)。我有两张桌子。
玩家(nr,player_id,team_id,姓名,积分,季节)
团队(team_id,team_name,season) - Ther是一个独特的team_id但是team_name可以根据不同的赛季而有所不同。
我想在特定赛季中记录单个球员_id的所有积分,我可以这样做:)
SELECT SUM(players.points),
players.player_id,
players.team_id,
players.season,
players.nr,
players.name
FROM players
WHERE season='2015/2016'
GROUP BY player_id
ORDER BY SUM(players.points) DESC
LIMIT 30
这项工作对我来说很完美:)但是,我无法想象如何加入团队表来获取
teams.team_name
对应于特定季节与WHERE clousure相匹配。在这种情况下,赛季=' 2015/2016'要有特色。如果我想从2015/2016获得积分,我需要从seasson 2015/2016获得team_name。我尝试了LEFT JOIN但没有成功:/
这是我的代码。
SELECT players.player_id,
players.nr,
players.name,
players.team_id,
SUM(players.points),
players.season
FROM players
WHERE players.sezon='2015/2016'
LEFT JOIN teams
ON (teams.team_id=players.team_id AND teams.season=players.season )
GROUP BY player_id
ORDER BY SUM(points) DESC
LIMIT 30
感谢您的努力。
答案 0 :(得分:0)
如果您不想在没有匹配球队记录的情况下显示球员记录,请使用INNER JOIN而不是LEFT JOIN
SELECT players.player_id,
players.nr,
players.name,
players.team_id,
SUM(players.points),
players.season,
teams.team_name
FROM players
LEFT JOIN teams
ON (teams.team_id = players.team_id AND teams.season = players.season )
WHERE players.season = '2015/2016'
GROUP BY player_id
ORDER BY SUM(points) DESC
LIMIT 30