之間的區別我想知道兩個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
確實如此,但是OP提到即使沒有where子句,他也會得到4000行而不是5000個。 –
我從第一個select語句中得到約4000行。當我把條件放入連接時,它可以正常工作。 – masfenix
這種情況下的外表是'TableB'的權利? – masfenix