Home > Database > Mysql Tutorial > body text

sql左,右和内,外接连详解

WBOY
Release: 2016-06-07 17:48:43
Original
1031 people have browsed it

内连接:INNER JOIN或者JOIN,把两个表中数据对应的数据查出来。 外连接:OUTER JOIN,以某个表为基础把对应数据查出来,分为左外连接和右外连接。 左外连接:LEFT JOIN或者LEFT OUTER JOIN,以某个表为基础把对应数据查出来。 右外连接:RIGHT JOIN或者RIGHT

student表
NO Name
1 a
2 b
3 c
4 d

grade表
NO Grade
1 90
2 98
3 95
5 90

内连接:查找条件中对应的数据,no4没有数据不列出来
语法: * from student inner join grade on student.no = grade.no
结果:
NO Name 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
结果:
NO Name 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
结果:
NO Name NO Grade
1 a 1 90
2 B 2 98
3 c 3 95
5 90


全连接
语法: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 将左连接和右连接合并后才可以

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template