2

询问:

SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,@row:=@row+1 rank 
FROM mismatch_user u 
LEFT JOIN ratingItems r ON u.user_id=r.uniqueName,
(SELECT @row:=0) pos 
ORDER BY votes DESC,rating DESC

输出:

user_id    votes   rating  rank
2            2      10      2
6            2      9       6
3            2      5       3
1            1      5       1
4            1      5       4
27           1      5       27
9            0      0       9

排名悲惨地没有告诉我真相,它基于user_id。谁能帮我?

4

2 回答 2

4

这有帮助吗?

select r.*, @row:=@row+1 rank 
  from 
    (SELECT u.user_id,r.totalVotes votes,r.totalPoints rating
       FROM mismatch_user u 
         LEFT JOIN ratingItems r ON u.user_id=r.uniqueName
    ) r
    join (SELECT @row:=0) pos 
ORDER BY r.votes DESC, r.rating DESC
于 2012-06-20T07:02:17.640 回答
1

您正在生成一个序列,这可能会导致棘手的行为。

如果您的查询是正确的,那么现在按排名排序的最安全方法是将其嵌入另一个 SELECT 中:

SELECT *
FROM (
    SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,@row:=@row+1 rank 
    FROM mismatch_user u 
    LEFT JOIN ratingItems r ON u.user_id=r.uniqueName,
    (SELECT @row:=0) pos 
    ORDER BY votes DESC,rating DESC) T
ORDER BY rank;
于 2012-06-20T07:00:05.570 回答