数据库
首页 > 数据库> > MySQL5.7实现ROW_NUMBER窗口函数功能SQL

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

作者:互联网

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

 

 

 

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

 

 

 


 

标签:name,--,MySQL5.7,NUMBER,rownumber,SQL,rn,SELECT
来源: https://www.cnblogs.com/panrg/p/14819002.html