gpt4 book ai didi

mysql - sql - 每个组的排序依据

转载 作者:行者123 更新时间:2023-11-29 11:23:47 24 4
gpt4 key购买 nike

我有一个如下所示的查询:

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

但是我希望它按国家/地区分组,以便用户可以按国家/地区的列表数量进行排名。有办法解决这个问题吗?

最佳答案

您可以有多个 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;

关于mysql - sql - 每个组的排序依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38549826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com