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)事务是一种机制,一个操作序列,包含一组操作指令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求(即要么全部执行,要么全部不执行)---------------- ...
随机推荐
- CentOS安装epel
Centos5安装 rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm rpm ...
- transition & transform
transition: 过渡时间 被改变属性 执行函数 延迟时间 transition:width 1s,height 2s 1s; transform: 平移(translate).缩放(scale ...
- JavaScript备忘录(3)——正则表达式
正则表达式是用来进行字符串匹配的. 定义正则表达式有两种方法:/wor/或者new RegExp("wor"). 使用方法 在JS中,使用正则表达式的方法有: 字符串的search ...
- PIN码计算锦集
1. 腾达,C8:3A:35开头的MAC有效~network路由,MAC有效~以及00B00C开头的MAC有效之外的请您自己发现算法..这里只公布三个MAC地址算法,其余也可以算~这里就不公布出来了. ...
- [原创]与来自facebook的朋友交流
与来自facebook的朋友交流 老板的儿子在facebook工作,现在正好有个假期回来,老总让我们部门与之进行一次交流.其实主要是他讲一下那边情况,然后我们准备些问题,多扩展一下我们见识. 流程 交 ...
- 表格CSS样式美化
1. 单像素边框CSS表格 这是一个很常用的表格样式. <!-- CSS goes in the document HEAD or added to your external styleshe ...
- Mina、Netty、Twisted一起学(二):TCP消息边界问题及按行分割消息
在TCP连接开始到结束连接,之间可能会多次传输数据,也就是服务器和客户端之间可能会在连接过程中互相传输多条消息.理想状况是一方每发送一条消息,另一方就立即接收到一条,也就是一次write对应一次rea ...
- 剑指架构师系列-Linux下的调优
1.I/O调优 CentOS下的iostat命令输出如下: $iostat -d -k 1 2 # 查看TPS和吞吐量 参数 -d 表示,显示设备(磁盘)使用状态:-k某些使用block为单位的列强制 ...
- Html5+css3+angularjs+jquery+webAPi 开发手机web(一)
前言 随着浏览器的发展 HTML5+CSS3 的使用也越来越广泛,一直想学这个,想学那个折腾下来几乎没学到什么东西.工作经验告诉我,要掌握一门技术,就需要在项目中去磨练, 所以我就准备开发一个手机端的 ...
- 专题——web.xml 中 url-pattern
一.映射什么? 一个请求发送到 servlet 容器,servlet 容器会将当前请求的 url 路径减去 协议.端口号.contextPath,剩下 servletPath 就是用来做 url-pa ...