SQL Server 2012 - 多表连接查询

-- 交叉连接产生笛卡尔值 (X*Y)
SELECT  * 
FROM    Student
        cross Join dbo.ClassInfo  
--另外一种写法
SELECT  * 
FROM    Student , ClassInfo  

-- 内连接 (Inner 可以省略)
SELECT  *
FROM    Student
        JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;

-- Inner Join 
SELECT  *
FROM    Student
        INNER  JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;

	  -- on 条件,通常是 主外键,但是不限于主外键
	  -- on 条件,允许有多个,而且可能是针对某个表的
SELECT  *
FROM    Student
        INNER  JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID
		AND dbo.Student.Class IN (1,2)  -- 针对Student表增加查询条件

-- 不等于 (笛卡尔值减去 相等的值)
SELECT  *
FROM    Student
        INNER  JOIN dbo.ClassInfo ON dbo.Student.Class <> dbo.ClassInfo.ID;

--自连接  从Class表中查询到Class所在的系
SELECT T1.* ,T2.ClassName FROM dbo.ClassInfo AS T1
		INNER JOIN dbo.ClassInfo AS T2 ON T1.PID=T2.ID

-- Left Join
SELECT  *
FROM    Student
        Left  JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;

-- Right Join
SELECT  *
FROM    Student
        RIGHT   JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;
-- 全外连接
SELECT  *
FROM    Student
        FULL OUTER  JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;

-- 外连接和内连接的区别是:
-- 内连接,on条件不符合的话,会过滤掉数据
-- 外连接,以保留表为主,on条件成立显示数据,否则显示NULL

--Union 纵向连接
SELECT  StuID,StuName,StuEnName,StuAge,StuBirthday
FROM    Student WHERE StuID<=2
UNION
SELECT StuID,StuName,StuEnName,StuAge,StuBirthday
FROM dbo.Student WHERE StuID>2

--Union 去重复
SELECT  StuSex
FROM    Student WHERE StuID<=2
UNION
SELECT StuSex
FROM dbo.Student WHERE StuID>2

--Union 显示全部
SELECT  StuSex
FROM    Student WHERE StuID<=2
UNION ALL
SELECT StuSex
FROM dbo.Student WHERE StuID>2

--Except 差集,排除
SELECT  StuID,StuName,StuEnName,StuAge,StuBirthday
FROM    Student 
Except
SELECT StuID,StuName,StuEnName,StuAge,StuBirthday
FROM dbo.Student WHERE StuID<=2

--Intersect 交集
SELECT  StuID,StuName,StuEnName,StuAge,StuBirthday
FROM    Student  WHERE StuID>=2
Except
SELECT StuID,StuName,StuEnName,StuAge,StuBirthday
FROM dbo.Student WHERE StuID<=3

  Union纵向查询的几个说明:

--Union 
--1,列的数目必须相同,对于没有的列,可以给默认的值,如NULL
SELECT StuID,StuName,StuSex,Class,Height FROM dbo.Student WHERE StuID<2
UNION 
SELECT  StuID,StuName,StuSex,Class,180 AS Height  FROM dbo.Student WHERE StuID=2
UNION 
SELECT  StuID,StuName,StuSex,Class,NULL  FROM dbo.Student WHERE StuID>2

--2,查询结果呈现的列名称是以第一列为准,如下面学生身高StuHeight
SELECT StuID,StuName,StuSex,Class,Height AS StuHeight FROM dbo.Student WHERE StuID<2
UNION 
SELECT  StuID,StuName,StuSex,Class,180 AS Height  FROM dbo.Student WHERE StuID=2
UNION 
SELECT  StuID,StuName,StuSex,Class,NULL AS hei FROM dbo.Student WHERE StuID>2

--3, 纵向对应的列的数据类型之间可以相互转换,如下 StuID和Height之间可以转换,可以纵向连接
SELECT StuID,StuName,StuSex,Class,Height AS StuHeight FROM dbo.Student WHERE StuID<2
UNION 
SELECT  Height,StuName,StuSex,Class,NULL AS hei FROM dbo.Student WHERE StuID>2

--4,对Union的查询结果进行排序,Order 写在最后
SELECT StuID,StuName,StuSex,Class,Height AS StuHeight FROM dbo.Student WHERE StuID<2
UNION 
SELECT  StuID,StuName,StuSex,Class,180 AS Height  FROM dbo.Student WHERE StuID=2
UNION 
SELECT  StuID,StuName,StuSex,Class,NULL AS hei FROM dbo.Student WHERE StuID>2
ORDER BY Class

  

posted @ 2017-01-25 18:00  i-shanghai  阅读(4343)  评论(0编辑  收藏  举报