2012-03-15 61 views
1

我想做一個查詢,我得到前3名撰稿人,即他們寫了最多的頁面/帖子。我會通過與每行關聯的用戶的session_id選擇數據,即他們寫入的頁面。我想選擇並排列數據庫中行數最多的前3名。我如何查詢這個?我在想......做一個查詢,以獲得數據行中排名前3的user_id id?

SELECT DISTINCT user_id 
FROM music_spot 
WHERE (need a condition here) 
ORDER BY (the person who contributed the most pages to the third 
LIMIT 3 

我怎麼會做這樣的事情?謝謝。

回答

2
SELECT user_id 
FROM music_spot 
WHERE session_id = 123 
ORDER BY count(user_id) desc 
group by user_id 
LIMIT 3 
+0

這得到所有user_ID的年代。現在,我怎麼才能得到每個id的一個出現,並按大多數行中顯示的順序排列它們。我試過DISTINCT,但那不起作用。 – Graham 2012-03-15 06:50:43

1
SELECT user_id, COUNT(post_id) 
FROM music_spot 
GROUP BY UserID 
ORDER BY COUNT(user_id) DESC 
LIMIT 0,3 
0
SELECT user_id, COUNT(row_id) 
FROM music_spot 
GROUP BY user_id 
ORDER BY COUNT(user_id) DESC 
LIMIT 0,3 
相關問題