# 分别求出变化前后的排名 然后再进行内连接即可# row_number()里面也可以用多个字段加减的表达式去进行排序#mysql如果相减出现负数,需要使用cast(字段名 as 类型)转换为signed类型的数SELECT t1.team_id,t1.name,cast(t1.r1 AS SIGNED)-cast(t2.r2 AS SIGNED) rank_diff
FROM(SELECT team_id,name,row_number()over(ORDERBY points desc,name asc) r1FROM TeamPoints
)t1
INNERJOIN(SELECT t.team_id,t.name,row_number()over(ORDERBY t.points+p.points_change desc,t.name asc) r2FROM TeamPoints tINNERJOIN PointsChange p ON t.team_id=p.team_id
)t2
ON t1.team_id=t2.team_id AND t1.name=t2.name
1.3 运行截图
2 7天内两次购买的用户
2.1 题目内容
2.1.1 基本题目信息
2.1.2 示例输入输出
2.2 示例sql语句
# 只要存在一周内购买的两次就行SELECT p1.user_id
FROM Purchases p1
INNERJOIN Purchases p2
ON p1.user_id=p2.user_id AND DATEDIFF(p1.purchase_date,p2.purchase_date)BETWEEN0AND7AND p1.purchase_id <> p2.purchase_id
GROUPBY p1.user_id
ORDERBY p1.user_id asc
2.3 运行截图
3 司机成为乘客的次数
3.1 题目内容
3.1.1 基本题目信息
3.1.2 示例输入输出
3.2 示例sql语句
# Write your MySQL query statement belowSELECT t.driver_id,IFNULL(count(r.ride_id),0) cnt
FROM(SELECTdistinct driver_idFROM Rides
)t
LEFTJOIN Rides r
ON t.driver_id=r.passenger_id
GROUPBY t.driver_id