where子句中条件的差异

时间:2017-07-23 12:45:45

标签: sql sql-server sql-server-2012

为什么以下查询条件会重复加入?这与第二个查询比较有什么影响吗?

1

select *  
FROM  A147_BOMDETAILS_VIEW T1 
  LEFT OUTER JOIN A147_INVENTQTYCOSTS_VIEW T2 
    ON (((T1.INVENTCOLORID_COMPONENT=T2.INVENTCOLORID 
    AND (T1.DATAAREAID = T2.DATAAREAID) 
    AND (T1.PARTITION = T2.PARTITION)) 
    AND (T1.INVENTSITEID=T2.INVENTSITEID 
    AND (T1.DATAAREAID = T2.DATAAREAID) 
    AND (T1.PARTITION = T2.PARTITION))) 
    AND (T1.ITEMID_COMPONENT=T2.ITEMID 
    AND (T1.DATAAREAID = T2.DATAAREAID) 
    AND (T1.PARTITION = T2.PARTITION)))?

2

select *  
FROM  A147_BOMDETAILS_VIEW T1 
  LEFT OUTER JOIN A147_INVENTQTYCOSTS_VIEW T2 
    ON T1.INVENTCOLORID_COMPONENT=T2.INVENTCOLORID 
    AND (T1.DATAAREAID = T2.DATAAREAID) 
    AND (T1.PARTITION = T2.PARTITION 
    AND T1.INVENTSITEID=T2.INVENTSITEID 
    and T1.ITEMID_COMPONENT=T2.ITEMID

谢谢, 工作

1 个答案:

答案 0 :(得分:1)

这两个查询返回相同的结果(从第一个查询中删除问号并在第二个查询结束时更正缺少的结束括号),但第一个版本中存在大量冗余条件。

如果您在括号中对条件进行分组,我希望看到or,但只有and。你可以抛弃括号并简化为:

select *
from A147_BOMDETAILS_VIEW T1
  left join A147_INVENTQTYCOSTS_VIEW T2
    on T1.INVENTCOLORID_COMPONENT = T2.INVENTCOLORID
   and T1.DATAAREAID = T2.DATAAREAID
   and T1.partition = T2.partition
   and T1.INVENTSITEID = T2.INVENTSITEID
   and T1.ITEMID_COMPONENT = T2.ITEMID