行视图查询连接中“where”和“And”之间的差异

时间:2015-01-22 12:42:04

标签: sql-server sql-server-2005 where inline

我在下面的这个帖子中读过,在查询中返回的数据没有区别,你在连接后有一个Where谓词或'And':

description of difference between 'where' and 'And' in join

但是,当我更改AND / WHERE时,我的查询中确实有不同的行数,这是在我的整体语句中使用内联视图查询。我注意到当我在行内查询中使用行号over语句时,问题就出现了。

我使用它的原因是将在线查询中的记录限制为仅第一个日期记录,因此我可以从外部查询返回后续但相关的记录。

然而,当我将WHERE更改为AND时,我会获得不同的行数。

我的查询与此类似:

SELECT DISTINCT
      table1.userID,
      table2.UniqueID,
      table3.entrydate,
      table2.entrytime,
      table4.changedatestart,
      table2.changetimestart,
      table5.changedateend,
      table2.changetimeend,
      table6.leavedate,
      table2.leavetime,
      table7.nationality_ID,
      table8.reg_code
      FROM
      table1 INNER JOIN table2 ON (table1.t1_KEY=table2.t2_KEY)
       RIGHT OUTER JOIN DATETIMETABLE  table4 ON (change_start_date_alias.DATEKEY=table2.change_date_start_KEY)
       RIGHT OUTER JOIN DATETIMETABLE  table5 ON (change_end_date_alias.DATEKEY=table2.change_date_end_KEY)
       RIGHT OUTER JOIN DATETIMETABLE  table3 ON (entry_date_alias.DATEKEY=table2.ENTRY_DATE_KEY)
       RIGHT OUTER JOIN DATETIMETABLE  table6 ON (leave_date_alias.DATEKEY=table2.LEAVE_DATE_KEY)
       RIGHT OUTER JOIN table7 ON (table7.nat_KEY=table2.nat_KEY)
       INNER JOIN table8 ON (table8.reg_DEPT_KEY=table2.reg_DEPT_KEY)
       RIGHT OUTER JOIN dbo.table9 ON (table9.leave_KEY=table2.leave_KEY)
    INNER JOIN
        (SELECT 
        table1.userID UID,
        ROW_NUMBER() OVER (PARTITION BY table1.userID ORDER BY table3.entrydate ASC) as Seq,
        table2.UniqueID "Unique_ID",
        table3.entrydate "entry_date",
        table2.entrytime "entry_time",
        table4.changedatestart "change_start_Date",
        table2.changetimestart "change Start Time",
        table5.changedateend "change End Date",
        table2.changetimeend "change End Time",
        table6.leavedate "leave Date",
        table2.leavetime "leave time",
        table7.nationality_ID "Nationality ID",
        table8.reg_code "Registration Code"
        FROM
        table1 INNER JOIN table2 ON (table1.t1_KEY=table2.t2_KEY)
        RIGHT OUTER JOIN DATETIMETABLE  table4 ON (change_start_date_alias.DATEKEY=table2.change_date_start_KEY)
        RIGHT OUTER JOIN DATETIMETABLE  table5 ON (change_end_date_alias.DATEKEY=table2.change_date_end_KEY)
        RIGHT OUTER JOIN DATETIMETABLE  table3 ON (entry_date_alias.DATEKEY=table2.ENTRY_DATE_KEY)
        RIGHT OUTER JOIN DATETIMETABLE  table6 ON (leave_date_alias.DATEKEY=table2.LEAVE_DATE_KEY)
        RIGHT OUTER JOIN table7 ON (table7.nat_KEY=table2.nat_KEY)
        INNER JOIN table8 ON (table8.reg_DEPT_KEY=table2.reg_DEPT_KEY)
        RIGHT OUTER JOIN dbo.table9 ON (table9.leave_KEY=table2.leave_KEY)
        WHERE table3.entrydate  BETWEEN  '20131201'  AND  '20140531'
        AND table8.reg_DESC  In  ( 'Value1','Value2','Value3','Value4','Value5'  )
        AND table9.leave_CODE IN ('11','15','16','22','25','27','54','57','66')
        )b
    ON b.UID = table1.userID
    **AND b.Unique_ID <> table2.UniqueID**
    AND b.Seq = 1
    AND b.[Registration Code] = table8.reg_code << If this line is commented out the same row count is returned whether WHERE/AND is used in not equal to ID statement:

AND/WHERE b.Unique_ID <> table2.UniqueID

但是,如果我重新引入最后一个lin并更改不等于ID语句中使用的WHERE / AND,则在使用AND语句而不是WHERE谓词时,它会提供更多的行。

1 个答案:

答案 0 :(得分:0)

在原始编辑中,您在示例中有inner join,因此我要求您发布原始代码。

请注意,RIGHT OUTER JOIN在处理INNER JOIN子句时与ON非常不同。在right outer join中,您可以获得右表中AND右表无论的所有记录。

所以,在下面的查询中,

SELECT * FROM Table1 RIGHT OUTER JOIN Table2 ON Table1.ID = Table2.Id
AND TABLE2.ID = 2

当您放置TABLE2.ID = 2时,您将获得Table2 甚至中的所有记录。它没有任何效果。然而,在下面的查询中,

SELECT * FROM Table1 RIGHT OUTER JOIN Table2 ON Table1.ID = Table2.Id
WHERE TABLE2.ID = 2

您只会获得TABLE2.ID = 2的记录。

但是对于内部联接,以下两个查询都是相似的:

SELECT * FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.Id
AND TABLE2.ID = 2
SELECT * FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.Id
WHERE TABLE2.ID = 2

因此,当您不谈论AND时,请小心处理RIGHT JOINS条件!

我曾经问过一个类似的问题here