图解T-SQL的联合查询

1. 概述

T-SQL微软产品旗下SQL Server系列数据库的数据库操作语言,是一门面向集合的语言,所以我们有必要把最集合概念搞清楚,如何从两个或两个以上的集合筛选出我们所需要的结果。

 

2. 查询分类

 

2.1 INNER JOIN

 

INNER_JOIN.png

  

--  方法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


2.2 LEFT JOIN

 

LEFT_JOIN.png

 

--  方法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

 

2.3 RIGHT JOIN

 

 

RIGHT_JOIN.png

 

--  方法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

 

 

2.4 OUTER JOIN

 

FULL_OUTER_JOIN.png 

 

SELECT  
    
< Select_List >
FROM  Table_A  AS  a
FULL OUTER JOIN  Table_B
    
ON  a. Key   =  B. KEY 

 

2.5 LEFT Excluding JOIN

 

 LEFT_EXCLUDING_JOIN.png

SELECT  
    
< 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

 

RIGHT_EXCLUDING_JOIN.png

 

SELECT  
    
< 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 

 

OUTER_EXCLUDING_JOIN.png 

 


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

 

【参考文章】 http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx

转载于:https://www.cnblogs.com/imumu/archive/2009/11/17/1604519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值