gpt4 book ai didi

MySQL:左连接和计数

转载 作者:行者123 更新时间:2023-11-29 07:22:57 25 4
gpt4 key购买 nike

我正在使用此查询来获取所有位置:

SELECT
*
FROM
locations
WHERE
cat='Bars'
ORDER BY
locationname
ASC

现在我还想显示“事件”表中位置信息旁边的现有事件数量。

Table 'locations': (all unique locations)
locationid|locationname|address|cat

Table 'events': (different eventid's but maybe multiple hits for locationid)
eventid|locationid|eventname|eventdate

知道如何解决这个问题吗?

最佳答案

你可以试试这个:

SELECT COUNT(e.eventid), l.locationid
FROM locations l
LEFT JOIN events e ON l.locationId = e.locationId
WHERE cat='Bars'
GROUP BY l.locationId
ORDER BY .locationname ASC

关于MySQL:左连接和计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35470772/

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