mysql实现分组聚合(ROW_NUMBER() OVER( [PARTITION BY column_1, column_2,…] [ORDER BY column_3,colum)
程序员文章站
2022-03-23 17:08:54
...
mysql实现row_number
row_number() over(partition by col1 order by col2),作为oracle常用的分析函数,先分组后排序
mysql的写法:
select if(@uid=t.clo1 and @cid=t.clo2,@rank:[email protected]+1,@rank:=1) as rank,t.*,@uid:=t.clo1,@cid:=t.clo2
from (select @uid:=null,@cid:=null,@rank:=0) r, (select clo1,clo2,city_name,clo3 from table_name order by clo1,clo2,clo3) t;
从表名为:table_name的表里取数据,根据字段clo1,clo2进行分组,根据字段clo3进行排序,rank是分组的行号,order by里一定要有两个分组字段及第三个排序字段。排完序只需要第一个后面就加’where rank=1’
套公式,try一try.
下一篇: jquery判断是否出现滚动条的方法
推荐阅读
-
MYSQL-实现sqlserver- row_number() over(partition by order by) 分组排序功能
-
MYSQL-实现sqlserver- row_number() over(partition by order by) 分组排序功能
-
mysql实现分组聚合(ROW_NUMBER() OVER( [PARTITION BY column_1, column_2,…] [ORDER BY column_3,colum)
-
【pandas】[4] DataFrame实现sql中row_number() over(partition by column_1 order by column_2)