我有一张父母的桌子

EventKey   Event Name
1001       Event 1
1002       Event 2
1003       Event 3

这是我的儿童桌
EventKey   EventAssignee
1001       Assignee 11
1001       Assignee 12
1002       Assignee 21
1002       Assignee 22

下面是我的SQL查询
select p.EventKey As Event_Key,
       p.Event_Name As EventName,
       (select count(*)
        from Child c
        where c.eventkey = p.eventkey) As Assignee_Count
from ParentTable p

这给了我一个SQL错误意外的标记子项。请告诉我哪里出错了
我希望输出是
Event_Key Event_Name Assignee_Count
1001      Event 1    2
1002      Event 2    2
1003      Event 3    0

最佳答案

试试看:

select p.EventKey As EventKey , p.EventName As EventName, count(c.assignee) As Assignee_Count
from Parent p left join child c on p.EventKey=c.EventKey
group by p.EventKey,p.EventName

10-08 07:07