sql常用操作之限量查询

题1

https://www.nowcoder.com/practice/fbe36305c6dd4954a05cc2f2f12e4f4a?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0

select uid, nick_name, register_time
from user_info
order by register_time asc
limit 3
select uid, nick_name, register_time
from (
    select uid, nick_name,register_time,
           ROW_NUMBER() over(order by register_time) as ranks # 列名要重命名。列名不能是row_number,否则报错
    from user_info
) as t
where ranks <= 3;
#limit 3;
with t as(
    select uid, nick_name,register_time,
           ROW_NUMBER() over(order by register_time) as ranks # 列名要重命名。列名不能是row_number,否则报错
    from user_info
)

select uid, nick_name, register_time
from t
where ranks <= 3;
# limit 3;

题2

https://www.nowcoder.com/practice/718d36d2667b48faa2168b6c1521816a?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
先生成临时表t,表示符合条件(求职为算法、答卷为算法、注册当天完成答卷)的人的信息。
最后分类,求出每人的成绩最大值再排序。

with t as(
    select t1.uid, level, register_time, score
    from user_info t1 join examination_info t2 join exam_record t3 on t1.uid = t3.uid and t2.exam_id = t3.exam_id
    where job="算法" and tag = '算法' and date(register_time) = date(submit_time)
)

select uid, level, register_time, max(score) as max_score
from t
group by uid
order by max_score DESC
limit 6, 3

limit用法:【select * from tableName limit i,n 】;
i : 为查询结果的索引值(默认从0开始);n : 为查询结果返回的数量

或直接写:

select t1.uid, level, register_time, max(score) as max_score
# from user_info t1 join examination_info t2 join exam_record t3 on t1.uid = t3.uid and t3.exam_id = t2.exam_id
from user_info t1 join exam_record using(uid) join examination_info using(exam_id)
where 
    job = "算法"       # job like '算法' 
    and tag = '算法'       # tag like '算法'
    and date(register_time) = date(submit_time) # 用date函数,即取年月日
#     and DATE_FORMAT(register_time, '%Y%m%d')=DATE_FORMAT(submit_time, '%Y%m%d')
group by uid
order by max_score desc
limit 6, 3  # 表示取第7~9条
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值