1. 概述
T-SQL微软产品旗下SQL Server系列数据库的数据库操作语言,是一门面向集合的语言,所以我们有必要把最集合概念搞清楚,如何从两个或两个以上的集合筛选出我们所需要的结果。
2. 查询分类
2.1 INNER JOIN
--
方法1(推荐)
SELECT
< Select_List >
FROM Table_A AS a
INNER JOIN Table_B
ON a. Key = B. KEY
-- 方法2(不推荐)
SELECT
< Select_List >
FROM Table_A AS a, Table_B AS b
WHERE a. KEY = b. Key
SELECT
< Select_List >
FROM Table_A AS a
INNER JOIN Table_B
ON a. Key = B. KEY
-- 方法2(不推荐)
SELECT
< Select_List >
FROM Table_A AS a, Table_B AS b
WHERE a. KEY = b. Key
2.2 LEFT JOIN
--
方法1(推荐)
SELECT
< Select_List >
FROM Table_A AS a
LEFT JOIN Table_B
ON a. Key = B. KEY
-- 方法2(不推荐)
SELECT
< Select_List >
FROM Table_A AS a, Table_B AS b
WHERE a. KEY *= b. Key
SELECT
< Select_List >
FROM Table_A AS a
LEFT JOIN Table_B
ON a. Key = B. KEY
-- 方法2(不推荐)
SELECT
< Select_List >
FROM Table_A AS a, Table_B AS b
WHERE a. KEY *= b. Key
2.3 RIGHT JOIN
--
方法1(推荐)
SELECT
< Select_List >
FROM Table_A AS a
RIGHT JOIN Table_B
ON a. Key = B. KEY
-- 方法2(不推荐)
SELECT
< Select_List >
FROM Table_A AS a, Table_B AS b
WHERE a. KEY =* b. Key
SELECT
< Select_List >
FROM Table_A AS a
RIGHT JOIN Table_B
ON a. Key = B. KEY
-- 方法2(不推荐)
SELECT
< Select_List >
FROM Table_A AS a, Table_B AS b
WHERE a. KEY =* b. Key
2.4 OUTER JOIN
SELECT
< Select_List >
FROM Table_A AS a
FULL OUTER JOIN Table_B
ON a. Key = B. KEY
< Select_List >
FROM Table_A AS a
FULL OUTER JOIN Table_B
ON a. Key = B. KEY
2.5 LEFT Excluding JOIN
SELECT
< Select_List >
FROM Table_A AS a
LEFT JOIN Table_B
ON a. Key = b. Key
WHERE b. Key IS NULL
< Select_List >
FROM Table_A AS a
LEFT JOIN Table_B
ON a. Key = b. Key
WHERE b. Key IS NULL
2.6 RIGHT Excluding JOIN
SELECT
< Select_List >
FROM Table_A AS a
RIGHT JOIN Table_B
ON a. Key = b. Key
WHERE a. Key IS NULL
< Select_List >
FROM Table_A AS a
RIGHT JOIN Table_B
ON a. Key = b. Key
WHERE a. Key IS NULL
2.7 OTHER Excluding JOIN
SELECT
< Select_List >
FROM Table_A AS a
FULL OUTER JOIN Table_B
ON a. Key = B. KEY
WHERE a.Key IS NULL
OR b.Key IS NULL
3. 查询举例
--
DROP TABLE #Zoo_A
CREATE TABLE #Zoo_A
(
Animal_ID INT NOT NULL PRIMARY KEY ,
Animal_Name NVARCHAR ( MAX ) NOT NULL
)
-- DROP TABLE #Zoo_B
CREATE TABLE #Zoo_B
(
Animal_ID INT NOT NULL PRIMARY KEY ,
Animal_Name NVARCHAR ( MAX ) NOT NULL
)
-- 动物园A中的动物
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 1 ,N ' 狮子 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 2 ,N ' 老虎 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 3 ,N ' 大象 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 4 ,N ' 长颈鹿 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 5 ,N ' 猴子 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 6 ,N ' 鸵鸟 ' )
-- 动物园B中的动物
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 1 ,N ' 狮子 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 2 ,N ' 老虎 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 3 ,N ' 大象 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 7 ,N ' 河马 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 8 ,N ' 猩猩 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 9 ,N ' 熊猫 ' )
-- 查询类型: INNER JOIN
-- 查询动物园A和动物园B相同的动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
INNER JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
-- 查询类型: LEFT JOIN
-- 查询动物园A中所有动物以及动物园B与动物园A相同的动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
LEFT JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
-- 查询类型: RIGHT JOIN
-- 查询动物园B中所有动物以及动物园A与动物园B相同的动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
RIGHT JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
-- 查询类型: FULL JOIN
-- 查询动物园A和动物园B的所有动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
FULL JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
-- 查询类型: LEFT Excluding JOIN
-- 查询只有动物园A有而动物园B没有动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
LEFT JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
WHERE zb. [ Animal_ID ] IS NULL
-- 查询类型: RIGH Excluding JOIN
-- 查询只有动物园B有而动物园A没有动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
RIGHT JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
WHERE [ za ] . [ Animal_ID ] IS NULL
-- 查询类型: RIGH Excluding JOIN
-- 查询动物园A以及动物园B所特有的动物,
-- 不包括动物园A和动物园B所共有的动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
FULL JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
WHERE [ za ] . [ Animal_ID ] IS NULL
OR [ zb ] . [ Animal_ID ] IS NULL
CREATE TABLE #Zoo_A
(
Animal_ID INT NOT NULL PRIMARY KEY ,
Animal_Name NVARCHAR ( MAX ) NOT NULL
)
-- DROP TABLE #Zoo_B
CREATE TABLE #Zoo_B
(
Animal_ID INT NOT NULL PRIMARY KEY ,
Animal_Name NVARCHAR ( MAX ) NOT NULL
)
-- 动物园A中的动物
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 1 ,N ' 狮子 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 2 ,N ' 老虎 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 3 ,N ' 大象 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 4 ,N ' 长颈鹿 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 5 ,N ' 猴子 ' )
INSERT INTO [ #Zoo_A ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 6 ,N ' 鸵鸟 ' )
-- 动物园B中的动物
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 1 ,N ' 狮子 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 2 ,N ' 老虎 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 3 ,N ' 大象 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 7 ,N ' 河马 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 8 ,N ' 猩猩 ' )
INSERT INTO [ #Zoo_B ] ( [ Animal_ID ] , [ Animal_Name ] )
VALUES ( 9 ,N ' 熊猫 ' )
-- 查询类型: INNER JOIN
-- 查询动物园A和动物园B相同的动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
INNER JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
-- 查询类型: LEFT JOIN
-- 查询动物园A中所有动物以及动物园B与动物园A相同的动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
LEFT JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
-- 查询类型: RIGHT JOIN
-- 查询动物园B中所有动物以及动物园A与动物园B相同的动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
RIGHT JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
-- 查询类型: FULL JOIN
-- 查询动物园A和动物园B的所有动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
FULL JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
-- 查询类型: LEFT Excluding JOIN
-- 查询只有动物园A有而动物园B没有动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
LEFT JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
WHERE zb. [ Animal_ID ] IS NULL
-- 查询类型: RIGH Excluding JOIN
-- 查询只有动物园B有而动物园A没有动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
RIGHT JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
WHERE [ za ] . [ Animal_ID ] IS NULL
-- 查询类型: RIGH Excluding JOIN
-- 查询动物园A以及动物园B所特有的动物,
-- 不包括动物园A和动物园B所共有的动物
SELECT
za. [ Animal_ID ] AS AnimalID_A,
za. [ Animal_Name ] AS AnimalName_A,
zb. [ Animal_ID ] AS AnimalID_B,
zb. [ Animal_Name ] AS AnimalName_B
FROM [ #Zoo_A ] AS za
FULL JOIN [ #Zoo_B ] AS zb
ON [ za ] . [ Animal_ID ] = [ zb ] . [ Animal_ID ]
WHERE [ za ] . [ Animal_ID ] IS NULL
OR [ zb ] . [ Animal_ID ] IS NULL
【参考文章】 http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx