码迷,mamicode.com
首页 > 数据库 > 详细

SQL SERVER LEFT JOIN, INNER JOIN, RIGHT JOIN

时间:2017-07-08 16:53:45      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:value   into   table   blog   varchar   ble   png   系统   img   

  • JOIN: 如果表中有至少一个匹配,则返回行
  • LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
  • RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
  • FULL JOIN: 只要其中一个表中存在匹配,就返回行
CREATE TABLE dbo.Student
(
 Sno int  null
,Name nvarchar(23) 
)
CREATE TABLE dbo.Score
(
 Sno int 
,Score INT
)
INSERT INTO dbo.Student(Sno,Name)VALUES(1,‘Jesse‘);
INSERT INTO dbo.Student(Sno,Name)VALUES(2,‘Jessca‘);
INSERT INTO dbo.Student(Sno,Name)VALUES(3,‘June‘);
INSERT INTO dbo.Student(Sno,Name)VALUES(4,‘Supper‘);

INSERT INTO dbo.Score(Sno,Score)VALUES (1,45)
INSERT INTO dbo.Score(Sno,Score)VALUES (1,56)
INSERT INTO dbo.Score(Sno,Score)VALUES (2,100)
INSERT INTO dbo.Score(Sno,Score)VALUES (3,25)  


 查看数据:

SELECT * FROM dbo.Student
SELECT * FROM dbo.Score

  技术分享

Left Join 会显示出左边的全部数据,右边如果没有对应的值,系统默认NULL

SELECT *
FROM dbo.Student a
LEFT JOIN dbo.Score b
ON a.Sno=b.Sno

  技术分享

INNER JOIN 显示交集

SELECT *
FROM dbo.Student a
INNER JOIN dbo.Score b
ON a.Sno=b.Sno

  技术分享

right join 右表全部显示,左表没有,默认设置为NULL

SELECT *
FROM dbo.Student a
RIGHT JOIN dbo.Score b
ON a.Sno=b.Sno

  

技术分享

FULL JOIN 左右边都显示,如果对方没有值得统一设置NULL

SELECT *
FROM dbo.Student a
FULL JOIN dbo.Score b
ON a.Sno=b.Sno

  技术分享

 

SQL SERVER LEFT JOIN, INNER JOIN, RIGHT JOIN

标签:value   into   table   blog   varchar   ble   png   系统   img   

原文地址:http://www.cnblogs.com/Jesse-Li/p/7131624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!