加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQLServer2005中的OUTER JOIN 和 INNER JOIN 精析(上)

发布时间:2020-12-12 15:36:01 所属栏目:MsSql教程 来源:网络整理
导读:本文分为上下两篇,上篇以一个小实例介绍各种 OUTER JOIN 和 INNER JOIN 操作 ,下篇针对各种 OUTER JOIN 和 INNER JOIN 操作在 SQLServer2005 中的逻辑查询处理步骤进行详解 ( 上 ) --table_one create table table_one (u_id int,u_name varchar(20)) inse

本文分为上下两篇,上篇以一个小实例介绍各种OUTER JOIN INNER JOIN 操作,下篇针对各种OUTER JOIN INNER JOIN 操作在SQLServer2005中的逻辑查询处理步骤进行详解

()

--table_one

create table table_one
(u_id int,u_name varchar(20))
insert table_one select 1,'AA'
union all select 2,'BB'
union all select 3,'CC'

如下图:

TABLEONE

?

--table_two

create table table_two
(u_id int,u_other varchar(50))
insert table_two select 1,'very good'
union all select 1,'thanks a lot'
union all select 1,'thanks a lot'
union all select 44,'very good'
union all select 33,'thanks a lot'
union all select 3,'very good'
union all select 3,'thanks a lot'
union all select 66,'very good'
union all select 66,'excellent'

如下图:?

TABLETWO

?? 下面是根据上面两个表的查询语句:

--------------------------------------外连接(OUTER JOIN )----------------------------

--LEFT OUTER JOIN? (也可以省略掉“OUTER”)

select a.u_id,a.u_name,b.u_other
from table_one a LEFT OUTER JOIN table_two b
ON? a.u_id=b.u_id

上面的“LEFT OUTER JOIN “效果如下:

?

LEFT OUTER JOIN633860519201607500

--FULL OUTER JOIN (也可以省略掉“OUTER”)

select a.u_id,b.u_other
from table_one a FULL OUTER JOIN table_two b
ON? a.u_id=b.u_id

效果如下:

?

FULL OUTER JOIN

--------------------------------------内连接(INNER JOIN )----------------------------

--INNER JOIN

select a.u_id,b.u_other
from table_one a INNER JOIN table_two b
ON? a.u_id=b.u_id

?

select a.u_id,b.u_other
from table_one a,table_two b
where? a.u_id=b.u_id

上面两条语句结果一样,效果如下:

?

INNER JOIN

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读