# 分为以下两者情况,分别考虑,然后union进行处理(有可能同时满足,需要去进行去重)# ①该用户在 三场及更多不同的 比赛中赢得 金牌(这些比赛可以不是连续的)分组然后数量大于3即可#连续三年以及更多,意味着只需要满足三年的情况就行#使用with创建临时表(查出每年用户是否获奖),inner join后面可以直接接上unionWITH T AS(SELECT u.user_id,c.contest_id
FROM Contests c
INNERJOIN Users u
ON c.gold_medal=u.user_id
OR c.silver_medal=u.user_id
OR c.bronze_medal=u.user_id
ORDERBY u.user_id,c.contest_id
)SELECT name,mail
FROM Users
WHERE user_id IN(SELECTdistinct t1.user_id
FROM T t1
INNERJOIN T t2
ON t1.user_id=t2.user_id AND t1.contest_id+1=t2.contest_id
INNERJOIN T t3
ON t2.user_id=t3.user_id AND t2.contest_id+1=t3.contest_id
UNIONSELECT gold_medal user_id
FROM Contests
GROUPBY gold_medal
HAVINGcount(contest_id)>=3)
1.3 运行截图
2 每天的最大交易
2.1 题目内容
2.1.1 基本题目信息
2.1.2 示例输入输出
2.2 示例sql语句
# Write your MySQL query statement belowSELECT t1.transaction_id
FROM(SELECT transaction_id,date_format(day,'%Y-%m-%d') d,amount
FROMTransactions)t1
INNERJOIN(SELECT t2.d,MAX(t2.amount) n
FROM(SELECT transaction_id,date_format(day,'%Y-%m-%d') d,amount
FROMTransactions)t2
GROUPBY t2.d
)t3
ON t1.d=t3.d AND t1.amount=t3.n
ORDERBY t1.transaction_id asc
2.3 运行截图
3 联赛信息统计
3.1 题目内容
3.1.1 基本题目信息1
3.1.2 基本题目信息2
3.1.3 示例输入输出
3.2 示例sql语句
# 注意表中是进球数,不是得分数# 分别求出客场时得分和主场时得分,两者合并后再分组即可# 按照字典顺序默认就是升序SELECT(SELECT team_name FROM Teams WHERE team_id=t1.team_id) team_name,count(*) matches_played,sum(t1.score) points,SUM(t1.n1) goal_for,SUM(t1.n2) goal_against,SUM(t1.n1)-SUM(t1.n2) goal_diff
FROM(SELECT home_team_id team_id,IF(home_team_goals>away_team_goals,3,IF(home_team_goals=away_team_goals,1,0)) score,
home_team_goals n1,away_team_goals n2
FROM Matches
UNIONALLSELECT away_team_id team_id,IF(home_team_goals<away_team_goals,3,IF(home_team_goals=away_team_goals,1,0)) score,
away_team_goals n1,home_team_goals n2
FROM Matches
)t1
GROUPBY t1.team_id
ORDERBY points desc,goal_diff desc,team_name asc