我很难创建SQL服务器查询。我有两个表格,下面是一对多的关系。我想提取所有活动,并在IsPrimary = true
{/ 1>}中包含子图片。
SELECT dbo.Activity.ID, dbo.Activity.Details, dbo.ActivityPicture.DateUploaded, dbo.ActivityPicture.IsPrimaryPicture, dbo.ActivityPicture.ImageData
FROM dbo.ReportTask
INNER JOIN dbo.ActivityPicture ON dbo.Activity.ID = dbo.ActivityPicture .ActivityID AND dbo.ActivityPicture.IsPrimaryPicture = 'true'
活动 -ID -details
ActivityPicture -ImageLocation -描述 -DateUploaded -IsPrimary -ActivityID
答案 0 :(得分:3)
将您的INNER JOIN
更改为LEFT OUTER JOIN
。这将返回第一个表中的所有项目,无论它们是否在第二个表中都有子项。
答案 1 :(得分:0)
SELECT dbo.Activity.ID, dbo.Activity.Details, dbo.ActivityPicture.DateUploaded, dbo.ActivityPicture.IsPrimaryPicture, dbo.ActivityPicture.ImageData FROM dbo.ReportTask LEFT JOIN dbo.ActivityPicture ON dbo.Activity.ID = dbo.ActivityPicture .ActivityID AND dbo.ActivityPicture.IsPrimaryPicture = 'true'
这将返回所有活动并包括活动图片。主图片为真。