Prayer

在一般中寻求卓越
posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

[转]左连接/右连接 实例

Posted on 2008-12-03 09:22 Prayer 阅读(332) 评论(0)  编辑 收藏 引用 所属分类: 数据库,SQL

左连接/右连接 实例

有两个表:

table1

id

name

password

1

aaa

a

2

bbb

b

3

ccc

c

table2

id

power

1

111000

2

000111

左连接sql语句:select table1.id,table1.name,table2.power from table1 left join table2 on table1.id = table2.id

得到如下结果:

id

name

power

1

aaa

000111

2

bbb

111000

3

ccc

(null)

右连接sql语句:select table1.id,table1.name,table2.power from table1 right join table2 on table1.id = table2.id

得到如下结果:

id

name

power

1

aaa

000111

2

bbb

111000

结果说明:左/右连接查询实际上是指定以哪个表的数据为准。

          左连接是只要左边的表中有记录,数据就能检索出来,而右边表中的记录必须在

          左边表中有记录才能检索出来;右连接同理。


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理