定义就不用说了吧!
测试数据 test_table
![](https://i-blog.csdnimg.cn/blog_migrate/9beca64d0e1a77746d0d4c109d357c10.jpeg)
create
table
test_table
(u_id
int
,u_name
varchar
(
20
))
insert
test_table
select
1
,
'
a1
'
union
all
select
2
,
'
b2
'
union
all
select
3
,
'
c3
'
;
测试数据 test_table_link
![](https://i-blog.csdnimg.cn/blog_migrate/8ea144d726f94b73de5aada623ddec1e.jpeg)
create
table
test_table_link
(u_id
int
,u_other
varchar
(
50
))
insert
test_table_link
select
1
,
'
each month
'
union
all
select
1
,
'
each day
'
union
all
select
1
,
'
each year
'
union
all
select
55
,
'
each day
'
union
all
select
55
,
'
each year
'
union
all
select
3
,
'
each day
'
union
all
select
3
,
'
each year
'
union
all
select
88
,
'
each day
'
union
all
select
88
,
'
each month
'
;
结果
![](https://i-blog.csdnimg.cn/blog_migrate/07f6dc07dbc47f11e11536abcfed7584.jpeg)
select
a.u_id,a.u_name,b.u_other
from
test_table a
LEFT
OUTER
JOIN
test_table_link b
ON
a.u_id
=
b.u_id;
测试环境为sql server 2005
原本以为只是显示test_table中的每3条记录,实际上不是这样的(为什么多了记录了呢?),只要第一个表(test_table)中有,而第二个表与之相关联得上的都会显示的,但是一定要在第一个表(test_table)中存在,如第二个表(test_table_link)中的uid为55,88的不会出现,若第二个表没有找到与第一个表相匹配的那当然为null了!right outer join就与之相反了.
SQL还真是强大啊~~
Trackback:http://www.cnblogs.com/jack-man/archive/2008/05/06/1185111.html