我正在使用此查询来获取所有位置:
SELECT
*
FROM
locations
WHERE
cat='Bars'
ORDER BY
locationname
ASC
现在,我也想从“事件”表中显示locationinfo旁边的现有事件数量。
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 - MySQL:左联接和计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35470772/