gpt4 book ai didi

SQL关于计数的问题

转载 作者:行者123 更新时间:2023-12-01 00:08:16 25 4
gpt4 key购买 nike

我想进行查询,以便我只能获取至少有 50 个地点的位置。

我有一张位置表:

Id, City, Country
1, Austin, USA
2, Paris, France

以及通过 Location_id 连接到 Locations 的 Places 表

Id, Name, Details, Location_id
1, The Zoo, blah, 2
2, Big Park, blah, 2

我可以像这样加入他们:

SELECT places.name, places.id, locations.country, locations.city从地方INNER JOIN 位置ON places.location_id = locations.id

如何只得到至少有50个地点的城市的结果,并按最大数量排序?

谢谢!

最佳答案

使用 GROUP BY with a HAVING条款。

SELECT locations.country, locations.city, COUNT(*)
FROM places
INNER JOIN locations ON places.location_id = locations.id
GROUP BY locations.country, locations.city
HAVING COUNT(*) >= 50

关于SQL关于计数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1321841/

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