微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLServer2005中的OUTER JOIN 和 INNER JOIN 精析上

本文分为上下两篇,上篇以一个小实例介绍各种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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐