2012-05-02 115 views
2

之間的區別我想知道兩個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 

他們不返回相同的行數。可以說,表1有5000行,表2也有5000行。第一條select join語句返回〜40000行(特定於我的查詢),然後應用where子句時,它返回大約2000行。

第二個select join語句返回5000行(原始數字)。這是我想要的數字。

但我似乎無法理解爲什麼會發生這種情況。我的知識和可視化聯接有點弱。

示例數據:請更改臨時表的名稱,它們是相同的。此外,我不能提供適當的列名,但已經編輯我的代碼來指示columsn我想匹配 這裏是表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'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

如果你把過濾條件在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行(或任何確切的行數是由本身後者查詢),那麼結果集是相同的。

+0

確實如此,但是OP提到即使沒有where子句,他也會得到4000行而不是5000個。 –

+0

我從第一個select語句中得到約4000行。當我把條件放入連接時,它可以正常工作。 – masfenix

+0

這種情況下的外表是'TableB'的權利? – masfenix