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;

  

posted @ 2019-02-21 16:42  liw66  阅读(419)  评论(0编辑  收藏  举报