【SQL】1407. 排名靠前的旅行者

题目描述

leetcode题目:1407. 排名靠前的旅行者



Code

写法一

先过滤,再连表

sql 复制代码
-- 写法一:先过滤再连表
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;

写法二

先连表,再过滤

sql 复制代码
-- 写法二:先连表再过滤
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;
相关推荐
小白菜又菜2 分钟前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
python·算法·leetcode
不想看见4043 分钟前
01 Matrix 基本动态规划:二维--力扣101算法题解笔记
c++·算法·leetcode
yzs8710 分钟前
PgSQL的外连接选择率计算
数据库
此生只爱蛋41 分钟前
【MySQL】变量
数据库·mysql
柒.梧.1 小时前
拆解Spring核心:IOC与AOP底层原理
数据库
Navigator_Z1 小时前
LeetCode //C - 964. Least Operators to Express Number
c语言·算法·leetcode
一个天蝎座 白勺 程序猿1 小时前
国产数据库破局之路——KingbaseES与MongoDB替换实战:从场景到案例的深度解析
开发语言·数据库·mongodb·性能优化·kingbasees·金仓数据库
骇城迷影1 小时前
代码随想录:二叉树篇(中)
数据结构·c++·算法·leetcode
thginWalker1 小时前
演进篇 · 维护篇
服务器·数据库