数据库中的连接操作

来源:互联网 发布:约翰威廉姆斯 知乎 编辑:程序博客网 时间:2024/05/22 16:51

1、左连接

左连接,保留表1所有内容,连接匹配表2的内容,若没有匹配,为null
例如:
A表

id Chinese 1 78 2 94 3 68

B表

id math 2 78 5 65 8 78
select * from A left join B on A.id=B.id

得到

id Chinese id math 1 78 null null 2 94 2 78 3 68 null null

2、右连接

右连接,与左连接类似,只是保留表2的所有内容,连接匹配表1的内容。

select * from A right join B on A.id=B.id

得到

id math id Chinese 2 78 2 94 5 65 null null 8 78 null null

3、内连接
内连接,相当于select … from … where

select * from A inner join B on A.id=B.id

得到

id Chinese id math 2 94 2 78

4、全连接
全连接,表1和表2的数据都显示

select * from A full join B on A.id=B.id

得到

id Chinese id math 1 78 null null 2 94 2 78 3 68 null null null null 5 65 null null 8 78
原创粉丝点击