mysql实现over()开窗函数功能
Posted siwluxuefeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql实现over()开窗函数功能相关的知识,希望对你有一定的参考价值。
mysql实现开窗函数功能:
参考:http://stackoverflow.com/questions/3333665/rank-function-in-mysql
CREATE TABLE person (id int, first_name varchar(20), age int, gender char(1));
INSERT INTO person VALUES (1, 'Bob', 25, 'M');
INSERT INTO person VALUES (2, 'Jane', 20, 'F');
INSERT INTO person VALUES (3, 'Jack', 30, 'M');
INSERT INTO person VALUES (4, 'Bill', 32, 'M');
INSERT INTO person VALUES (5, 'Nick', 22, 'M');
INSERT INTO person VALUES (6, 'Kathy', 18, 'F');
INSERT INTO person VALUES (7, 'Steve', 36, 'M');
INSERT INTO person VALUES (8, 'Anne', 25, 'F');
---------------------------------------------
--不是做分区,只做排名---
SELECT first_name,
age,
gender,
@curRank := @curRank + 1 AS rank
FROM person p, (SELECT @curRank := 0) r
ORDER BY age;
--分区排名--
SELECT a.first_name,
a.age,
a.gender,
count(b.age)+1 as rank
FROM person a
left join person b
on a.age>b.age and a.gender=b.gender
group by a.first_name,
a.age,
a.gender
以上是关于mysql实现over()开窗函数功能的主要内容,如果未能解决你的问题,请参考以下文章