数据库内连接、外连接(左连接、右连接、全连接)

本文详细介绍了SQL中的不同连接类型,包括内连接、左连接、右连接及全连接的概念与应用场景。通过具体的例子展示了如何使用这些连接来从不同的表中检索数据,并解释了每种连接返回结果的不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

供自己参考:
[color=red]内连接:把两个表中数据对应的数据查出来
外连接:以某个表为基础把对应数据查出来(全连接是以多个表为基础)[/color]

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

grade表
no grade
1 90
2 98
3 95

[b]内连接 inner join(查找条件中对应的数据,no4没有数据不列出来)[/b]
语法: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

[b]左连接(左表中所有数据,右表中对应数据)[/b]
语法: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
[color=red]4 d[/color]

[b]右连接(右表中所有数据,左表中对应数据)[/b]
语法: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 [color=red] 95[/color]

[color=red]全连接
语法: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[/color]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值