MYSQL order 、group 与row_number详解

一、order by

order by A ASC, B DESC,C ASC …

上述语句会先按照A排序,当A相同的时候再按照B排序,当B相同的再按照C排序,并会不按照ABC组合一起排序

二、group by

group by A,B,C…

select 中的字段必须是group by中的字段,但是聚合函数中的参数字段没有必须出现在 group by中

select A , B ,count(*) from t1 group by A,B,C
select A , B ,C,count(*) from t1 group by A,B,C
select A , B ,count(C) from t1 group by A,B,C
# 语法错误,应该使用having
select A , B ,count(C) as countABC  from t1  where countABC >3  group by A,B,C
select A , B ,count(C) as countABC  from t1  having countABC >3  group by A,B,C 

select A , B ,count(C) from t1 group by A,B,C

select A ,count(*) from t1 group by A 与 select A ,count(A) from t1 group by A 
与 
select A ,count(B) from t1 group by A  效果一致

select A ,count(distinct B) from t1 group by A 
与
select A ,B,count(*) from t1 group by A ,B 效果一致

select A ,sum(B) from t1 group by A 
select A ,max(B) from t1 group by A 
select A ,min(B) from t1 group by A 

三、row_number 窗口排名函数

row_number() over ( [partition by column] order by order_expression [asc|desc]) as ranking
该方法常用于排名、连续登陆数统计,部门业绩排名

row_number 无并列排名,强制连续序号 例如 1,2,3
rank 允许并列排名,后续序号不连续 例如 1,1,3
dense_rank() 允许并列排名,后续序号连续 例如 1,1,2

count(*) 统计所有行数,包括NULL
count(column) 统计column字段非NULL的行数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值