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)事务是一种机制,一个操作序列,包含一组操作指令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求(即要么全部执行,要么全部不执行)---------------- ...
随机推荐
- Java知多少(110)数据库之插入记录
插入数据表记录有3种方案 一.使用Statement对象 实现插入数据表记录的SQL语句的语法是: insert into 表名(字段名1,字段名2,……)value (字段值1,字段值2,……) 例 ...
- 优化LibreOffice如此简单
对于开源软件的支持者和粉丝来说,LibreOffice 无疑是 Microsoft Office 的最佳替代品,而且它已在过去的许多版本迭代中迎来了许多巨大改进.然而,通过用户的手动配置,我们还是有办 ...
- @media 手机与IPAD与PC
@media screen and (min-width: 769px) { bindCard{height:28.5em} } @media screen and (min-device-width ...
- Arcgis报错: Bad login user Failed to execute (CreateEnterpriseGeodatabase).
在使用工具Create Enterprise Geodatabase的时候报错Bad login user,开始怀疑为密码错误,然后反复在plsql中尝试发现并没有错误,很疑惑,然后去官网查询: Er ...
- Android通过编码实现GPS开关
在Android 2.2以后才可使用 import android.content.ContentResolver; import android.content.Context; import an ...
- win7中的画图
下午做一个图像篡改定位实验,在win7下对图像进行修改,然后通过其最低位判断篡改位置. 程序如何都调试不出来.反复简化程序,发现win7中的画图对图像存在优化. 当更改图像后,会自动调整当前图像的显示 ...
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了 ...
- Redis设计与实现-附加功能
发布与订阅 redis订阅分为精准的频道订阅与模糊匹配的模式订阅: redis将所有频道的订阅关系都保存在服务器状态pubsub_channels字典里,键是频道名,值是一个记录所有订阅此频道的客户端 ...
- Build 2016概览
很快Microsoft Build 2016马上就要开始,在直播放出来之前,微软已经提前把本次大会期间的所有课程列表放了出来,你可以在这里看到: https://channel9.msdn.com/E ...
- 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract
[源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...
1、inner join