-2

我试图理解为什么以下两个 SQL 语句会给出不同的结果。第一个按预期工作,第二个不产生任何记录。

 Select * from Jet.LayoutListItemEntity_Default itemDefault
 Join  Jet.LayoutListEntity  b
 on itemDefault.UiKey = b.UiKey
 Left join (Select * from Jet.LayoutListItemEntity where DomainId =2) item
 on itemDefault.BindingPath = item.BindingPath
 where item.DomainId is null
 and b.DomainId = 2


 Select * from Jet.LayoutListItemEntity_Default itemDefault
 Join  Jet.LayoutListEntity  b
 on itemDefault.UiKey = b.UiKey
 Left join Jet.LayoutListItemEntity  item
 on itemDefault.BindingPath = item.BindingPath
 where item.DomainId is null
 and item.DomainId = 2
 and b.DomainId = 2

主要的区别是人们把 theitem.DomainId = 2放在最后,而不是放在它自己的选择中。在我看来,它们会产生相同的结果。

格雷格

4

2 回答 2

4

这些条件是互斥的:

where item.DomainId is null
 and item.DomainId = 2

item.DomainID不能同时是NULL2

您可以将错误的条件移至JOIN条件:

Select * from Jet.LayoutListItemEntity_Default itemDefault
 Join  Jet.LayoutListEntity  b
 on itemDefault.UiKey = b.UiKey
 Left join Jet.LayoutListItemEntity  item
 on itemDefault.BindingPath = item.BindingPath
    and item.DomainId = 2
 where item.DomainId is null
 and b.DomainId = 2
于 2013-09-25T01:04:41.720 回答
1

您的WHERE条件在第二个查询中被弄乱了——要使这些语句相同,and item_DomainId = 2请将JOIN.

 Select * from Jet.LayoutListItemEntity_Default itemDefault
     Join  Jet.LayoutListEntity  b
         on itemDefault.UiKey = b.UiKey
     Left join Jet.LayoutListItemEntity  item
         on itemDefault.BindingPath = item.BindingPath and item.DomainId = 2
 where item.DomainId is null
     and b.DomainId = 2
于 2013-09-25T01:07:24.897 回答