SQL语句连接语句

时间:2015-04-07 14:08:04

标签: sql sql-server

我很难创建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

2 个答案:

答案 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'

这将返回所有活动并包括活动图片。主图片为真。