`
myfight_xuan
  • 浏览: 11816 次
  • 性别: Icon_minigender_1
  • 来自: 大连
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

数据库内连接、左连接、右连接

阅读更多

数据库内连接、左连接、右连接

1.内连接我们通常用的连接,表表连接只显示交集数据

2.外连接分左外连接 table1 left outer join on table2

和右外连接table1 right outer join  on table2 和全连接

table1 full outer join on table2

2.1左外连接就是在等值连接的基础上加上主表中的未匹配数据
2.2右外连接是在等值连接的基础上加上被连接表的不匹配数据
2.3全外连接是在等值连接的基础上将左表和右表的未匹配数据都加上.

内连接:把两个表中数据对应的数据查出来
外连接:以某个表为基础把对应数据查出来(全连接是以多个表为基础)

student表
no name
1 a
2 b
3 c
4 d

grade表
no grade
1 90
2 98
3 95

内连接 inner join(查找条件中对应的数据,no4没有数据不列出来)
语法:select * from student inner join grade on student.no = grade.no
结果
student.no name grade.no grade
1 a 1 90
2 b 2 98
3 c 3 95

左连接(左表中所有数据,右表中对应数据)
语法:select * from student left join grade on student.no = grade.no
结果:
student.no name grade.no grade
1 a 1 90
2 b 2 98
3 c 3 95
4 d

右连接(右表中所有数据,左表中对应数据)
语法:select * from student right join grade on student.no = grade.no
结果:
student.no name grade.no grade
1 a 1 90
2 b 2 98
3 c 3 95

全连接
语法:select * from student full join grade on student.no = grade.no
结果:
no name grade
1 a 90
2 b 98
3 c 95
4 d
1 a 90
2 b 98
3 c 95

注:access 中不能直接使用full join ,需要使用union all 将左连接和右连接合并后才可以

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics