你可能想看看windowing functions如果你的MySQL版本支持他們......
SELECT m.member_id AS member_id,
(SUM(c.vote_value) + SUM(c.best)*10) AS total,
RANK() OVER (ORDER BY (SUM(c.vote_value) + SUM(c.best)*10)) as ranking
FROM comments c
LEFT JOIN members m ON c.author_id = m.member_id
GROUP BY c.author_id
ORDER BY total DESC;
另一種可能性是:
SELECT m.member_id AS member_id,
(SUM(c.vote_value) + SUM(c.best)*10) AS total,
(SELECT count(distinct <column you want to rank by>)
FROM comments c1
WHERE c1.author_id = m.member_id) as ranking
FROM comments c
LEFT JOIN members m ON c.author_id = m.member_id
GROUP BY c.author_id
ORDER BY total DESC;
注:圍繞此問題有很多開放性問題,但上述兩種技術是確定排名的一般方法。你需要改變上面的內容以適合你的確切需要,因爲我對member_id的排名構成了一點模糊。
我不明白 - 查詢看起來很好。也許一些樣本數據和預期產出會有所幫助? – 2010-05-25 20:59:02
Rank,如何顯示排名,以及指定的UID – Kenan 2010-05-25 20:59:46