gpt4 book ai didi

mysql - SQL Count() child Rows 基于来自 Parent 的值

转载 作者:行者123 更新时间:2023-11-29 04:08:38 25 4
gpt4 key购买 nike

我有一个父表

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

关于mysql - SQL Count() child Rows 基于来自 Parent 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19713858/

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