题目描述
leetcode题目:1407. 排名靠前的旅行者
Code
写法一
先过滤,再连表文章来源:https://www.toymoban.com/news/detail-842388.html
-- 写法一:先过滤再连表
select name, ifnull(summ, 0) as travelled_distance
from Users
left join(
select user_id, sum(distance) as summ
from Rides
group by user_id
) A
on Users.id = A.user_id
order by summ desc, name asc;
写法二
先连表,再过滤文章来源地址https://www.toymoban.com/news/detail-842388.html
-- 写法二:先连表再过滤
select name, ifnull(sum(distance), 0) as travelled_distance
from Users U
left join Rides R
on U.id = R.user_id
group by R.user_id
order by travelled_distance desc, name;
到了这里,关于【SQL】1407. 排名靠前的旅行者的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!