Which statement is true about an inner join specified in the WHERE clause of a query?
A. It must have primary-key and foreign-key constraints defined on the columns used in the join condition.
B. It requires the column names to be the same in all tables used for the join conditions.
C. It is applicable for equijoin and nonequijoin conditions.
D. It is applicable for only equijoin conditions.
Correct Answer: C
Section: (n one) Explanation:
left join(左聯接) 返回包括左表中的所有記錄和右表中聯結字段相等的記錄
right join(右聯接) 返回包括右表中的所有記錄和左表中聯結字段相等的記錄
inner join(等值連接) 只返回兩個表中聯結字段相等的行,就是等連接
left join/right join/inner join操作演示
表A記錄如下:
aID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115
表B記錄如下:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408
實驗如下:
1. left join
sql語句如下:
SELECT * FROM A
LEFT JOIN B
ON A.aID = B.bID
結果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
5 a20050115 NULL NULL
(所影響的行數爲 5 行)
結果說明:
left join是以A表的記錄爲基礎的,A可以看成左表,B可以看成右表,left join是以左表爲準的.
換句話說,左表(A)的記錄將會全部表示出來,而右表(B)只會顯示符合搜索條件的記錄(例子中爲: A.aID = B.bID).
B表記錄不足的地方均爲NULL.
2. right join
sql語句如下:
SELECT * FROM A
RIGHT JOIN B
ON A.aID = B.bID
結果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
NULL NULL 8 2006032408
(所影響的行數爲 5 行)
結果說明:
仔細觀察一下,就會發現,和left join的結果剛好相反,這次是以右表(B)爲基礎的,A表不足的地方用NULL填充.
3.inner join
sql語句如下:
SELECT * FROM A
INNERJOIN B
ON A.aID = B.bID
結果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
結果說明:
很明顯,這裏只顯示出了 A.aID = B.bID的記錄.這說明inner join並不以誰爲基礎,它只顯示符合條件的記錄.