SQL Server join介绍
介绍Inner Join(可以省略Inner,平常经常inner,就是inner join), Full Out Join,Cross Join,Left Join, Right Join区别。
create table Customers (Cust_Id int, Cust_Name varchar(10))
insert Customers values (1, 'Craig')
insert Customers values (2, 'John Doe')
insert Customers values (3, 'Jane Doe') create table Sales (Cust_Id int, Item varchar(10))
insert Sales values (2, 'Camera')
insert Sales values (3, 'Computer')
insert Sales values (3, 'Monitor')
insert Sales values (4, 'Printer')
Customers 表数据:

Sales 表数据:1、inner join
两边都有的才筛选出来(Inner join 对表无顺序概念)
--Inner Join
--两边都有的才筛选出来(Inner join 对表无顺序概念)
select
s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
from Sales S inner join Customers C
on S.Cust_Id = C.Cust_Id --平时经常就简单使用单使用join字段,就是inner join
--如下实例: 结果集跟使用inner join 一模一样
select
s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
from Customers c join sales s
on c.cust_id = s.cust_id

2、Full Out Join
两边都列出来,能匹配就匹配,不匹配的用NULL列出 (Full Out Join 对表无顺序概念)
--Full Out Join
--两边都列出来,能匹配就匹配,不匹配的用NULL列出 (Full Out Join 对表无顺序概念)
select
s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
from Sales S full outer join Customers C
on S.Cust_Id = C.Cust_Id

3、Cross Join
列出两边所有组合,也叫笛卡尔集A.Rows * B.Rows (Cross Join 对表无顺序概念)
--Cross Join
--列出两边所有组合,也叫笛卡尔集A.Rows * B.Rows (Cross Join 对表无顺序概念)
select
s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
from Sales S cross join Customers C

4、Left Join
以左边的表为主表,列出主表所有记录,能匹配就匹配,不匹配的用NULL列出
Left Join 对表有顺序概念 前面是主表 后面是副表
--Left Join
--以左边的表为主表,列出主表所有记录,能匹配就匹配,不匹配的用NULL列出
--Left Join 对表有顺序概念 前面是主表 后面是副表
-- 实例-1、Sales 为主表 (两个表的数据都显示)
select
s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
from sales s left join Customers c
on c.cust_id = s.cust_id

2)、Customers 为主表
-- 实例-2、Customers 为主表
select
s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
from Customers c left join sales s
on c.cust_id = s.cust_id

5、Right Join
以右边的表为主表,列出主表所有记录,能匹配就匹配,不匹配的用NULL列出
Right Join 对表有顺序概念 前面是副表 后面是主表
--Right Join
--以右边的表为主表,列出主表所有记录,能匹配就匹配,不匹配的用NULL列出
--Right Join 对表有顺序概念 前面是副表 后面是主表
-- 实例.1、 sales 为主表
select
s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
from sales s right join Customers c
on c.cust_id = s.cust_id

2)、Salves为主表
-- 实例.2、 sales 为主表
select
s.Cust_id as Sales_Cust_id ,S.item as Sales_item,
C.Cust_id as Customers_Cust_id,c.Cust_name as Customers_Cust_name
from Customers c right join sales s
on c.cust_id = s.cust_id

SQL Server join介绍的更多相关文章
- SQL Server Join方式
原文:SQL Server Join方式 0.参考文献 Microsoft SQL Server企业级平台管理实践 看懂SqlServer查询计划 1.测试数据准备 参考:Sql Server中的表访 ...
- 【译】索引进阶(一):SQL SERVER索引介绍
[译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:http://www.sqlservercentral.com/articles/Stairway+Series/7 ...
- SQL Server 全文索引介绍(转载)
概述 全文引擎使用全文索引中的信息来编译可快速搜索表中的特定词或词组的全文查询.全文索引将有关重要的词及其位置的信息存储在数据库表的一列或多列中.全文索引是一种特殊类型的基于标记的功能性索引,它是由 ...
- SQL SERVER 触发器介绍
什么是触发器 触发器对表进行插入.更新.删除的时候会自动执行的特殊存储过程.触发器一般用在check约束更加复杂的约束上面.触发器和普通的存储过程的区别是:触发器是当对某一个表进行操作.诸如:upda ...
- SQL Server常见问题介绍及快速解决建议
前言 本文旨在帮助SQL Server数据库的使用人员了解常见的问题,及快速解决这些问题.这些问题是数据库的常规管理问题,对于很多对数据库没有深入了解的朋友提供一个大概的常见问题框架. 下面一些问题是 ...
- SQL Server 索引介绍
数据库索引是对数据表中一个或多个列的值进行排序的结构,就像一本书的目录一样,索引提供了在行中快速查询特定行的能力 详细出处参考:http://www.jb51.net/article/30950.ht ...
- Step1:SQL Server 复制介绍
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 前言(Introduction) 复制逻辑结构图(Construction) 系列文章索引(Catalog) 总结&am ...
- 【能力提升】SQL Server常见问题介绍及高速解决建议
前言 本文旨在帮助SQL Server数据库的使用人员了解常见的问题.及高速解决这些问题.这些问题是数据库的常规管理问题,对于非常多对数据库没有深入了解的朋友提供一个大概的常见问题框架. 以下一些问题 ...
- SQL server 事务介绍,创建与使用
事务(Transaction)事务是一种机制,一个操作序列,包含一组操作指令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求(即要么全部执行,要么全部不执行)---------------- ...
随机推荐
- VR是TAA的终结者吗?
在刚刚发布的Unreal Engine 4.14中,其第一个重要的特性就是增加了在VR开发中对Forward Shading的支持.我们都知道在很多方面Deferred Shading都优于Forwa ...
- TL(简单)
TL time limit per test 2 seconds memory limit per test 256 megabytes input standard input output sta ...
- 自定义ISPF面板
1)登录的时候可以看到登录执行的PROCEDURE,此处为DBSPROC 2.登录后,进入SDSF,再进入LOG,输入命令TOP,再输入命令F JOB,按F11把屏幕向右翻页,看到哪下界面 找到//I ...
- 【转】mysql如何跟踪执行的sql语句
转自http://blog.csdn.net/testcs_dn/article/details/18791815 在SQL SERVER下跟踪sql采用事件探查器,而在mysql下如何跟踪sql呢? ...
- Hadoop第4周练习—HDFS读写文件操作
1 运行环境说明... 3 :编译并运行<权威指南>中的例3.2. 3 内容... 3 2.3.1 创建代码目录... 4 2.3.2 建立例子文件上传到hdfs中... 4 ...
- 4、总结:基于Oracle Logminer数据同步
最近开发Oracle数据同步功能,做了些调研和验证,这个工作过去有段时间,怕时间长了忘记,故用博客共享出来.在这系列中共写了三篇文章,第一篇是写LogMiner配置及使用,第二篇是测试了LogMine ...
- java基础---->java中正则表达式二
跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...
- sql server service broker中调用存储过程执行跨库操作,不管怎么设置都一直提示 服务器主体 "sa" 无法在当前安全上下文下访问数据库 "dbname"。
用sql server自带的消息队列service borker,调用存储过程中,执行了一个跨库的操作,先是用了一个用户,权限什么都给够了,但是一直提示 服务器主体 "user" ...
- 希望早几年知道的5个Unix命令
原文: http://spin.atomicobject.com/2013/09/09/5-unix-commands/ 希望早几年知道的5个Unix命令 使用*nix系统已经有一段时间了.但是还是有 ...
- 三分 --- ZOJ 3203 Light Bulb
Light Bulb Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3203 Mean: ...
1、inner join