答案 0 :(得分:1)
我很高兴有一天MySQL5x被MySQL8 +完全取代,并且MySQL中有真正的窗口函数。但是,直到那一天,对于MySQL 5x / MariaDB <10.2,您可以使用类似的东西:
SELECT username
, progress_percent
, gold_count
, @uRank := @uRank + 1 AS theRank
FROM mytable t, ( SELECT @uRank := 0 ) r
ORDER BY progress_percent DESC, gold_count DESC, modify_date DESC
编辑:显然直到10.2之前,窗口函数才添加到MariaDB中。 :-S
如果您使用的是MySQL 8+或MariaDB 10.2+,则可以使用“等级窗口”功能:
SELECT username
, progress_percent
, gold_count
, ROW_NUMBER() OVER (ORDER BY progress_percent DESC, gold_count DESC, modify_date DESC ) AS theRank
FROM mytable t
ORDER BY theRank
编辑2:
对于特定用户,只需将以上查询放入子查询中,然后SELECT
放入您想要的Rank
。
SELECT s1.username, s1.progress_percent, s1.gold_count
FROM (
[above query here]
) s1
WHERE s1.theRank = <theRankYouWant>
或
WHERE s1.username = <theUsernameYouWant>