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

 



posted @   大空白纸  阅读(977)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示