MySQL5.7实现ROW_NUMBER窗口函数功能SQL

1、原始数据:SELECT * FROM test_rownumber;

MySQL5.7实现ROW_NUMBER窗口函数功能SQL

 

 

 

2、ROW_NUMBER功能实现SQL

SELECT * FROM
(
SELECT
-- 当变量@name等于字段值的时候,变量@rn加1,如果不相等赋值为 1
@rn := CASE WHEN @name = NAME THEN @rn + 1 ELSE 1 END AS rn ,
-- 把name字段值赋值于变量@name
@name:=name as name1 ,
num
FROM
( SELECT * FROM test_rownumber ORDER BY name,num DESC ) a,
-- 初始化一个变量值
(SELECT @rn = 0,@name=0) b
) c

MySQL5.7实现ROW_NUMBER窗口函数功能SQL

 

 

 


 

上一篇:RN框架在京东无线端的实践


下一篇:一款轻阅读应用ReadIT,记录我的RN躺坑之旅