join子句和where子句中的条件之间的差异

时间:2012-05-02 16:04:30

标签: sql-server-2008

我想知道两个SELECT语句之间的差异(视觉上)是最好的。

SELECT P.* and X.* 
FROM Table1 P
LEFT OUTER JOIN 
    Table2 X ON
    and  p.B=x.B
and  p.C=x.C
and  p.F=x.F
and  p.D=x.D 

WHERE X.F= 1 and X.E=4 

SELECT P.* and X.* 
FROM Table1 P
LEFT OUTER JOIN 
    (SELECT * FROM Table2 WHERE X.F= 1 and X.E =4) X
    and  p.B=x.B
and  p.C=x.C
and  p.F=x.F
and  p.D=x.D

它们不会返回相同数量的行。让我们说Table1有5000行,Table2也有5000行。第一个select join语句返回~40000行(特定于我的工作查询),然后当应用where子句时,它返回大约2000行。

第二个select join语句返回5000行(原始数字)。这是我想要的数字。

但我似乎无法理解为什么会这样。我的知识和可视化连接有点弱。

示例数据:请更改临时表的名称,它们是相同的。此外,我无法提供正确的列名,但已编辑我的代码以指示我想要匹配的columsn 这是TABLE1:

    SELECT * INTO #tmp_GridResults_1
FROM (
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'1' AS [E], N'2' AS [F] ) t;
SELECT [ID], [B], [C], [D], [E], [F]
FROM #tmp_GridResults_1

DROP TABLE #tmp_GridResults_1
GO

这是表2: ---------------#tmp_GridResults_1 ---------------

SELECT * INTO #tmp_GridResults_1
FROM (
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'0' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'0' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'0' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'0' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'0' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'0' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'0' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'0' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'1' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'1' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'2' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'2' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'2' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'2' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'2' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'2' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'2' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'2' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'2' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'2' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'2' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'2' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'3' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'3' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'3' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'3' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'3' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'3' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'3' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'3' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'3' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'3' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'3' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'3' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'4' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'4' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'4' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'4' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'4' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'4' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'4' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'4' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'4' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'4' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'4' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'4' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'98' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'98' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'-1' AS [D], N'98' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'-1' AS [D], N'98' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'98' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'98' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'0' AS [D], N'98' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'0' AS [D], N'98' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'98' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'98' AS [E], N'1' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'1' AS [C], N'1' AS [D], N'98' AS [E], N'2' AS [F] UNION ALL
SELECT N'0' AS [ID], N'P' AS [B], N'2' AS [C], N'1' AS [D], N'98' AS [E], N'2' AS [F] ) t;
SELECT [ID], [B], [C], [D], [E], [F]
FROM #tmp_GridResults_1

DROP TABLE #tmp_GridResults_1
GO

1 个答案:

答案 0 :(得分:1)

如果在WHERE子句中放置过滤条件,并且该条件规定了连接的“外部”表的条件,则可以有效地将该外部连接转换为内部连接。针对外连接表的过滤条件属于连接,而不属于where。

此版本产生了多少行?

SELECT P.*, X.* 
FROM Table1 P
LEFT OUTER JOIN Table2 X
    on P.Id = X.id 
    and P.name = X.name 
    and P.age = X.age 
    AND X.bar = 1 
    and X.name ='value';

理论上,这应该产生> =第一个语句中的行数,以及=第二个语句中的行数。

如果您想将此与现有结果进行比较,您可以执行以下操作:

SELECT P.*, X.* 
FROM Table1 P
LEFT OUTER JOIN Table2 X
    on P.Id = X.id 
    and P.name = X.name 
    and P.age = X.age 
    AND X.bar = 1 
    and X.name ='value'
EXCEPT
SELECT P.* and X.*
FROM Table1 P
LEFT OUTER JOIN 
    (SELECT * FROM Table2 WHERE X.bar = 1 and X.name ='value') X
    on P.Id = X.id 
    and P.name = X.name 
    and P.age = X.age;

如果没有结果,他们就是一样的。您也可以采用相反的方式:

SELECT P.*, X.* 
FROM Table1 P
LEFT OUTER JOIN Table2 X
    on P.Id = X.id 
    and P.name = X.name 
    and P.age = X.age 
    AND X.bar = 1 
    and X.name ='value'
INTERSECT
SELECT P.* and X.*
FROM Table1 P
LEFT OUTER JOIN 
    (SELECT * FROM Table2 WHERE X.bar = 1 and X.name ='value') X
    on P.Id = X.id 
    and P.name = X.name 
    and P.age = X.age;

如果你得到5000行(或者后者查询本身的确切行数),那么结果集是相同的。