SQL LEFT OUTER JOIN与Where子句有关的问题

时间:2019-05-27 15:03:58

标签: sql sql-server tsql join

我在数据库中有两个表。产品表包含产品信息,另一个表SiteProduct包含第一个表的ID,该表名为ProductID,另外一个SiteID,用于存储目录中哪些站点具有哪些产品。我需要在显示所有产品的网站上创建一个网格,然后检查或取消选中该网站的目录中是否包含该产品。我需要在一个查询中执行此操作。请在下面查看我的查询,我正在尝试使用左外部联接来联接“产品”表和“ SiteProduct”表。但是,使用此查询返回的唯一结果是在SiteProduct表中具有SiteID 100的记录的产品。因此,说有100种产品,而SiteProduct表中有50个SiteID为100的产品,当我返回50个结果时,需要查看全部100。我需要查询以显示所有产品,然后显示一个空值,或者如果SiteID100在SiteProduct表中没有该条目,则返回一个值。同样,这必须在一个查询中完成,我不想为每个产品都对SiteProduct表进行后续的SELECT调用。理想情况下,我希望此功能可以作为左外连接使用,但可以接受其他可能性。

请在下面查看我的查询,谢谢。

SELECT this_.ID                  as ID110_3_,
       this_.Name                as Name110_3_,
       this_.Description         as Descript3_110_3_,
       this_.RxStatement         as RxStatem4_110_3_,
       this_.CreatedDate         as CreatedD5_110_3_,
       this_.Weight              as Weight110_3_,
       this_.UPC                 as UPC110_3_,
       this_.UOM                 as UOM110_3_,
       this_.LastModified        as LastModi9_110_3_,
       this_.IsDeleted           as IsDeleted110_3_,
       this_.DeletedDate         as Deleted11_110_3_,
       this_.Url                 as Url110_3_,
       this_.CreatedByUserId     as Created13_110_3_,
       this_.Status              as Status110_3_,
       this_.CategoryID          as CategoryID110_3_,
       this_.ManufacturerID      as Manufac16_110_3_,
       this_.CreatedBySiteID     as Created17_110_3_,
       this_.DistributorFeedGuid as Distrib18_110_3_,
       sp3_.ProductID            as ProductID5_,
       sp3_.ID                   as ID5_,
       sp3_.ID                   as ID143_0_,
       sp3_.Name                 as Name143_0_,
       sp3_.Description          as Descript3_143_0_,
       sp3_.HasSpecial           as HasSpecial143_0_,
       sp3_.IsAdvanced           as IsAdvanced143_0_,
       sp3_.SpecialDescription   as SpecialD6_143_0_,
       sp3_.SpecialTitle         as SpecialT7_143_0_,
       sp3_.ProductNumber        as ProductN8_143_0_,
       sp3_.Price                as Price143_0_,
       sp3_.SalePrice            as SalePrice143_0_,
       sp3_.StoreDescription     as StoreDe11_143_0_,
       sp3_.Tagline              as Tagline143_0_,
       sp3_.LastModified         as LastMod13_143_0_,
       sp3_.CreatedDate          as Created14_143_0_,
       sp3_.RentalPrice          as RentalP15_143_0_,
       sp3_.IsRental             as IsRental143_0_,
       sp3_.IsFeaturedProduct    as IsFeatu17_143_0_,
       sp3_.ShowPrice            as ShowPrice143_0_,
       sp3_.RequestType          as Request19_143_0_,
       sp3_.MetaTitle            as MetaTitle143_0_,
       sp3_.MetaDesc             as MetaDesc143_0_,
       sp3_.MetaKeywords         as MetaKey22_143_0_,
       sp3_.RequireRX            as RequireRX143_0_,
       sp3_.[Order]              as Order24_143_0_,
       sp3_.BrightreeItemID      as Brightr25_143_0_,
       sp3_.LowQuantityThreshold as LowQuan26_143_0_,
       sp3_.UOM                  as UOM143_0_,
       sp3_.BTIsRental           as BTIsRental143_0_,
       sp3_.WeeklyRentalRate     as WeeklyR29_143_0_,
       sp3_.MonthlyRentalRate    as Monthly30_143_0_,
       sp3_.SiteID               as SiteID143_0_,
       sp3_.ProductID            as ProductID143_0_,
       cat2_.ID                  as ID25_1_,
       cat2_.Name                as Name25_1_,
       cat2_.ImageID             as ImageID25_1_,
       manu1_.ID                 as ID85_2_,
       manu1_.Name               as Name85_2_
FROM   Catalog.[Product] this_
       left outer join Catalog.[SiteProduct] sp3_
         on this_.ID = sp3_.ProductID
       inner join Catalog.Category cat2_
         on this_.CategoryID = cat2_.ID
       inner join [Manufacturer] manu1_
         on this_.ManufacturerID = manu1_.ID
WHERE  sp3_.SiteID = 100
ORDER  BY this_.Name asc

1 个答案:

答案 0 :(得分:0)

我最终在where子句中使用多个in语句解决了这个问题。