数据库
首页 > 数据库> > Mysql查询指定用户并列排名 类似rank函数

Mysql查询指定用户并列排名 类似rank函数

作者:互联网

SELECT
	total.*
FROM
	(
		SELECT
			obj.uid,
			obj.score,
			CASE
		WHEN @rowtotal = obj.score THEN
			@rownum
		WHEN @rowtotal := obj.score THEN
			@rownum :=@rownum + 1
		WHEN @rowtotal = 0 THEN
			@rownum :=@rownum + 1
		END AS rownum
		FROM
			(
				SELECT
					uid,
					score
				FROM
					t
				ORDER BY
					score DESC
			) AS obj,
			(
				SELECT
					@rownum := 0 ,@rowtotal := NULL
			) r
	) AS total
WHERE
	total.uid = 222;

  

标签:obj,uid,Mysql,rank,score,并列,rownum,rowtotal,SELECT
来源: https://www.cnblogs.com/liw66/p/10413563.html