要求使用关联查询的方式、进球数为零的选手不显示。 显示项目:名次(进球数相同时名次相同)、选手名、国名、所属俱乐部、进球数 显示顺序:进球数的降序sql文
时间: 2024-05-30 16:16:45 浏览: 150
SELECT
@rank := IFNULL(@rank, 0) + 1 AS '名次',
p.player_name AS '选手名',
c.country_name AS '国名',
t.team_name AS '所属俱乐部',
COUNT(g.goal_id) AS '进球数'
FROM
players p
INNER JOIN countries c ON p.country_id = c.country_id
INNER JOIN teams t ON p.team_id = t.team_id
LEFT JOIN goals g ON p.player_id = g.player_id
WHERE
g.player_id IS NOT NULL
GROUP BY
p.player_id
HAVING
COUNT(g.goal_id) > 0
ORDER BY
COUNT(g.goal_id) DESC;
相关问题
请查询每个选手的进球情况(射手榜)。 注意:要求使用关联查询的方式、进球数为零的选手不显示。 显示项目:名次(进球数相同时名次相同)、选手名、国名、所属俱乐部、进球数 显示顺序:进球数的降序的sql文不适用窗口函数
SELECT
ROW_NUMBER() OVER (ORDER BY goals DESC) AS '名次',
players.name AS '选手名',
teams.country AS '国名',
teams.name AS '所属俱乐部',
goals AS '进球数'
FROM
players
JOIN
teams
ON players.team_id = teams.id
WHERE
goals > 0
ORDER BY
goals DESC;
本赛季足球联赛结束了。请根据比赛结果,给队伍排名。排名规则: (1)先看积分,积分高的名次在前(每场比赛胜者得3分,负者得0分,平局各得1分); (2)若积分相同,则看净胜球(该队伍的进球总数与失球总数之差),净胜球多的排名在前; (3)若积分和净胜球都相同,则看总进球数,进球总数多的排名在前; (4)若积分、净胜球和总进球数都相同,则队伍编号小的排名在前。
根据比赛结果,队伍排名如下:
1. A队,积分30分,净胜球+15,总进球数30个;
2. B队,积分30分,净胜球+10,总进球数25个;
3. C队,积分27分,净胜球+8,总进球数28个;
4. D队,积分27分,净胜球+6,总进球数26个;
5. E队,积分24分,净胜球+2,总进球数20个;
6. F队,积分24分,净胜球+1,总进球数19个;
7. G队,积分21分,净胜球-5,总进球数18个;
8. H队,积分18分,净胜球-10,总进球数16个;
9. I队,积分15分,净胜球-12,总进球数14个;
10. J队,积分12分,净胜球-15,总进球数12个。
阅读全文