表的连接分为内连和外连
内连接
内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选,我们前面文章的查询都是内连接,也是在开发过程中使用的最多的连接查询。
语法
select 字段 from 表1 inner join 表2 on 连接条件 and 其他条件;
案例
- 显示
smith的名字和部门名称
用之前的方法
mysql> select ename, dname from emp, dept where emp.deptno=dept.deptno and ename='smith'; +-------+----------+ | ename | dname | +-------+----------+ | smith | research | +-------+----------+ 1 row in set (0.00 sec)
用标准内连接
mysql> select ename, dname from emp inner join dept on emp.deptno=dept.deptno and ename='smith'; +-------+----------+ | ename | dname | +-------+----------+ | smith | research | +-------+----------+ 1 row in set (0.00 sec)
两种方式混合使用,更好的区分笛卡尔积和筛选条件
mysql> select ename, dname from emp inner join dept on emp.deptno=dept.deptno where ename='smith'; +-------+----------+ | ename | dname | +-------+----------+ | smith | research | +-------+----------+ 1 row in set (0.00 sec)
外连接
外连接分为左外连接和右外连接
左外连接
如果联合查询,左侧的表完全显示我们就说是左外连接。
语法
select 字段名 from 表名1 left join 表名2 on 连接条件
案例
前期准备:
mysql> create table stu (id int, name varchar(30));-- 学生表 query ok, 0 rows affected (0.03 sec) mysql> desc stu; +-------+-------------+------+-----+---------+-------+ | field | type | null | key | default | extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | yes | | null | | | name | varchar(30) | yes | | null | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> insert into stu values(1,'jack'),(2,'tom'),(3,'kity'),(4,'nono'); query ok, 4 rows affected (0.01 sec) records: 4 duplicates: 0 warnings: 0 mysql> select * from stu; +------+------+ | id | name | +------+------+ | 1 | jack | | 2 | tom | | 3 | kity | | 4 | nono | +------+------+ 4 rows in set (0.00 sec) mysql> create table exam (id int, grade int); -- 成绩表 query ok, 0 rows affected (0.02 sec) mysql> desc exam; +-------+---------+------+-----+---------+-------+ | field | type | null | key | default | extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | yes | | null | | | grade | int(11) | yes | | null | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> insert into exam values(1, 56),(2,76),(11, 8); query ok, 3 rows affected (0.00 sec) records: 3 duplicates: 0 warnings: 0 mysql> select * from exam; +------+-------+ | id | grade | +------+-------+ | 1 | 56 | | 2 | 76 | | 11 | 8 | +------+-------+ 3 rows in set (0.00 sec)
- 查询所有学生的成绩,如果这个学生没有成绩,也要将学生的个人信息显示出来
mysql> select * from stu left join exam on stu.id=exam.id; +------+------+------+-------+ | id | name | id | grade | +------+------+------+-------+ | 1 | jack | 1 | 56 | | 2 | tom | 2 | 76 | | 3 | kity | null | null | | 4 | nono | null | null | +------+------+------+-------+ 4 rows in set (0.00 sec)
右外连接
如果联合查询,右侧的表完全显示我们就说是右外连接。
语法
select 字段 from 表名1 right join 表名2 on 连接条件;
案例
- 对
stu表和exam表联合查询,把所有的成绩都显示出来,即使这个成绩没有学生与它对应,也要显示出来
mysql> select * from stu right join exam on stu.id=exam.id; +------+------+------+-------+ | id | name | id | grade | +------+------+------+-------+ | 1 | jack | 1 | 56 | | 2 | tom | 2 | 76 | | null | null | 11 | 8 | +------+------+------+-------+ 3 rows in set (0.00 sec)
练习
列出部门名称和这些部门的员工信息,同时列出没有员工的部门
运用左外连接
mysql> select dept.deptno, dname, ename from dept left join emp on dept.deptno=emp.deptno order by dept.deptno asc; +--------+------------+--------+ | deptno | dname | ename | +--------+------------+--------+ | 10 | accounting | clark | | 10 | accounting | miller | | 10 | accounting | king | | 20 | research | jones | | 20 | research | smith | | 20 | research | adams | | 20 | research | scott | | 20 | research | ford | | 30 | sales | martin | | 30 | sales | allen | | 30 | sales | james | | 30 | sales | blake | | 30 | sales | ward | | 30 | sales | turner | | 40 | operations | null | +--------+------------+--------+ 15 rows in set (0.00 sec)
运用右外连接
mysql> select dept.deptno, dname, ename from emp right join dept on dept.deptno=emp.deptno order by dept.deptno asc; +--------+------------+--------+ | deptno | dname | ename | +--------+------------+--------+ | 10 | accounting | clark | | 10 | accounting | miller | | 10 | accounting | king | | 20 | research | jones | | 20 | research | smith | | 20 | research | adams | | 20 | research | scott | | 20 | research | ford | | 30 | sales | martin | | 30 | sales | allen | | 30 | sales | james | | 30 | sales | blake | | 30 | sales | ward | | 30 | sales | turner | | 40 | operations | null | +--------+------------+--------+ 15 rows in set (0.00 sec)
⚠️ 写在最后:以上内容是我在学习以后得一些总结和概括,如有错误或者需要补充的地方欢迎各位大佬评论或者私信我交流!!!
到此这篇关于mysql表的内联和外联区别案例解析的文章就介绍到这了,更多相关mysql内联和外联内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论