2016-07-24 79 views
0

我有一個查詢,看起來像這樣:SQL - 爲了通過對每個組

SELECT 
    USER .id, 
    listing.country, 
    count(listing.id) AS count 
FROM USER 
INNER JOIN listing ON USER.id = listing.user_id 
GROUP BY listing.country, USER.id 
ORDER BY count DESC; 

,結果我得到這個樣子的:

enter image description here

但是我想它按國家分組,以便用戶可以根據其列表數量按國家排列。有沒有辦法來解決這個問題?

+0

你能提供的結果的一個例子設置你希望得到什麼? – Reisclef

回答

1

你可以有多個ORDER BY列:

SELECT `user`.`id`, 
     `listing`.`country`, 
     count(`listing`.`id`) as `count` 
FROM `user` 
INNER JOIN `listing` ON (`user`.`id` = `listing`.`user_id`) 
GROUP BY `listing`.`country,` `user`.`id` 
ORDER BY `country`, `count` DESC;