gpt4 book ai didi

sql - 如何计算一对多的关系

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

ReporterTblAttachmentTbl 有一对多的关系.

ReporterTbl , 我有一个 ID (101) 并且我可以有 AttachmentTbl不止一个AttachmentReporterTbl.Id 相关

SELECT     
ISNULL(ReporterTbl.Id, 0) AS Id,
CONVERT(char(10), ReporterTbl.StartDate, 101) AS StartDate,
ISNULL(ReporterTbl.PriorityId, 0) AS PriorityId,
ISNULL(dbo.ReporterTbl.PriorityDesc, '') AS PriorityDesc,
(select
ReporterTbl.Id,
COUNT(dbo.AttachmentTbl.Id) AS attachment_Id
FROM
dbo.AttachmentTbl RIGHT OUTER JOIN
ReporterTbl ON dbo.AttachmentTbl.Id = ReporterTbl.Id
GROUP BY ReporterTbl.Id) AS IsAttachment
)

基本上,我想知道的是 ReporterTbl.ID ,多少 Attachment我有吗?

表结构:
 ReporterTbl

Id int {**PrimaryKey**}
StartDate datetime
PriorityId int
PriorityDesc varchar(500

AttachmentTbl:

AttachmentId indentity
Id {**FK to ReproterTbl**}
Filename
Content
...

最佳答案

select r.id, count(a.id) as Count
from ReporterTbl r
left outer join AttachmentTbl a on r.id = a.id
group by r.id

关于sql - 如何计算一对多的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3679777/

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