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)事务是一种机制,一个操作序列,包含一组操作指令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求(即要么全部执行,要么全部不执行)---------------- ...
随机推荐
- 比特(bit)与字节(byte)区别,站位比较
“字节”(Byte) “比特”(Bit) 当你进行网络下载的时候它们会经常出现,同时你获取的速度指示也都以比特/每秒或者字节/每秒来显示. 现在就来弄清楚比特(Bit).字节(Byte)和千字节(Kb ...
- 利用SVN工具下载OpenCore代码
OpenCore原来使用的是CVS管理代码的.从09年起,更换用SVN管理代码,大家可以用TortoiseSVN软件下载代码,网址是:http://tortoisesvn.net/,安装后tortoi ...
- 手机浏览器JS识别
识别方法:采用Fiddler 抓包工具 侦测手机http链接,抓取http头 查看 工具:Fiddler 1:Fiddler配置 允许远程设备连接,配置端口为默认8888(确保8888端口没有被其他进 ...
- poj 3486 A Simple Problem with Integers(树状数组第三种模板改段求段)
/* 树状数组第三种模板(改段求段)不解释! 不明白的点这里:here! */ #include<iostream> #include<cstring> #include< ...
- ruby -- 进阶学习(一)subdomain配置与实现
今天和guanMac童鞋研究的subdomain配置终于有点头绪~~ 之所以会遇到种种难题,个人总结了一下,第一本人太菜,第二英语不好 贴一下guanMac童鞋配置小结的链接:http://my.eo ...
- php分享三十三:用php中的register_shutdown_function和fastcgi_finish_request
已前言 在php中又两个方法都是在请求快结束的时候执行.方法名分别是 register_shutdown_function和fastcgi_finish_request.虽然执行的时机差不多,但是功能 ...
- 编译Libgdiplus遇到的问题
https://github.com/mono/libgdiplus/releases 下载最新版本 解压并执行 ./autogen.sh,在执行此步时遇到如下问题,并帖出解决办法 问题:执行 ...
- How to implement an algorithm from a scientific paper
Author: Emmanuel Goossaert 翻译 This article is a short guide to implementing an algorithm from a scie ...
- Linux bash - 常用操作命令
一.终端基础 本文摘录一些本人在学习Linux(CentOS 6.6) bash命令,并且会不定期保持更新. 在此先介绍一下Linux shell终端的常规命令输入格式,如下图: 上图中root是用户 ...
- Hadoop第1~2周练习—Hadoop1.X和2.X安装
练习题目 Hadoop1.X安装 2.1 准备工作 2.1.1 硬软件环境 2.1.2 集群网络环境 2.1.3 安装使用工具 2.2 环境搭建 2.2.1 安 ...
1、inner join