SQL Server 2012 多表连接查询功能实例代码
2019/6/30 17:02:42
本文主要是介绍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
以上所述是小编给大家介绍的SQL Server 2012 多表连接查询功能实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
这篇关于SQL Server 2012 多表连接查询功能实例代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-08Docker下的SqlServer发布订阅启用
- 2023-06-05Docker安装MS SQL Server并使用Navicat远程连接
- 2023-05-25深入浅出 SQL Server CDC 数据同步
- 2023-05-12通过空间占用和执行计划了解SQL Server的行存储索引
- 2023-04-24以SQLserver为例的Dapper详细讲解
- 2022-11-30SQL server高级函数查询
- 2022-11-26SQL SERVER数据库服务器CPU不能全部利用原因分析
- 2022-11-21SQL Server 时间算差值/常用函数
- 2022-11-20调试Archery连接SQL Server提示驱动错误
- 2022-10-22SQL Server 完整、差异备份+完整、差异还原(详细讲解,规避错误)