2016-07-24 56 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;