mysql实现row_number() 功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql实现row_number() 功能相关的知识,希望对你有一定的参考价值。
查询test表按group_id分组取sort_id前100个
SELECT id AS ‘原数据ID‘,group_id AS ‘分组ID‘,sort_id AS ‘排序条件‘,num AS ‘排名‘
FROM
(SELECT @row_number := CASE WHEN @customer_no = group_id THEN @row_number + 1 ELSE 1 END AS num /*如果同group_id那么排名加1,否则更新为1*/
,@customer_no := group_id AS group_id /*重新加载group_id*/
,id , sort_id /*原有数据*/
FROM test,( SELECT @customer_no := 0,@row_number := 0) AS t
ORDER BY group_id ,sort_id DESC ,id)a
WHERE num<=100
以上是关于mysql实现row_number() 功能的主要内容,如果未能解决你的问题,请参考以下文章
MYSQL-实现sqlserver- row_number() over(partition by order by) 分组排序功能