我有两张桌子:
event: id_event, description
guest: id_guest, id_event
我正在使用以下查询:
SELECT id_event, description FROM event LEFT JOIN guest ON event.id_event = guest.id_event;
我需要获取事件在来宾表中出现的次数,如果没有出现,则为零。
答案 0 :(得分:2)
您走在正确的轨道上,只需要group by
来计算:
SELECT e.id_event, e.description, count(id_guest) as NumTimes
FROM event e LEFT JOIN
guest g
ON e.id_event = g.id_event
GROUP BY e.id_event, e.description;
您还应该习惯使用表别名。它们使查询更容易编写和阅读。