牛客网SQL刷题 总结

WHERE 关键字后面不能与聚合函数一起使用,解决方法:使用 `having`,参考:https://www.cnblogs.com/mafeng/p/9198514.html

SQL执行顺序

(8) SELECT (9) DISTINCT 
    (1) FROM 
    (3)  JOIN 
    (2)    ON 
    (4) WHERE 
    (5) GROUP BY 
    (6) WITH {CUBE|ROLLUP} 
    (7) HAVING 
    (10) ORDER BY 
    (11) LIMIT

求最大值:

1,使用聚合函数。max( )

2,倒序,取第一条。

order by xxx desc
limit 1;

union和union all的区别

union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。

利用case when end来分段查看:

select device_id,gender,
case
when age<20 then '20岁以下'
when age<25 then '20-24岁'
when age>=25 then '25岁及以上'
else '其他'
end age_cut
from user_profile;

日期函数:

select day(date) as day,
count(question_id) as question_cnt
from question_practice_detail
where year(date) = 2021 and month(date) = 08
group by date

分割字符串函数:substring_index(tr,delim,count)
tr是字符串,delim是分隔符号,count是负数表示从右边开始数。

 

posted on   passionConstant  阅读(43)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示