MySQL多表查询讲解( 二 )

正确的查询方式是:以两表中相互关联的字段作为查询条件进行查询 。
mysql> select * from employee,dept where employee.dept_id = dept.did;查询结果
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+| id | name| age | sex | salary| hire_date| dept_id | did | dname|+----+-----------+-----+-----+----------+------------+---------+-----+-----------+|1 | 菜虚鲲 |20 |2 | 10000.00 | 2020-01-10 |1 |1 | 研发部 ||2 | 奥力给 |30 |1 | 18000.00 | 2020-01-08 |1 |1 | 研发部 ||3 | 老八|28 |1 | 7000.00| 2020-01-07 |1 |1 | 研发部 ||4 | 小张|25 |1 | 8000.00| 2020-01-10 |1 |1 | 研发部 ||5 | 小红|20 |2 | 6000.00| 2020-01-05 |2 |2 | 人事部 ||6 | 小丽|23 |2 | 6500.00| 2020-01-05 |2 |2 | 人事部 ||7 | 小花|21 |2 | 5500.00| 2020-01-10 |2 |2 | 人事部 ||8 | 马小跳 |25 |1 | 7000.00| 2020-01-01 |3 |3 | 测试部 ||9 | 张大骚 |30 |1 | 9000.00| 2020-01-07 |3 |3 | 测试部 || 10 | 马冬梅 |31 |2 | 5000.00| 2020-01-07 |4 |4 | 销售部 |+----+-----------+-----+-----+----------+------------+---------+-----+-----------+10 rows in set (0.00 sec)输出结果为两个集合的交集 。

MySQL多表查询讲解

文章插图
 
3. Innner内连接查询3.1 语法select 字段1,字段2... from 表1 inner join 表2 on [条件];3.2 实战查询员工和部门信息
select * from employee inner join dept on employee.dept_id = dept.did; 输出结果
mysql> select * from employee inner join dept on employee.dept_id = dept.did; +----+-----------+-----+-----+----------+------------+---------+-----+-----------+| id | name| age | sex | salary| hire_date| dept_id | did | dname|+----+-----------+-----+-----+----------+------------+---------+-----+-----------+|1 | 菜虚鲲 |20 |2 | 10000.00 | 2020-01-10 |1 |1 | 研发部 ||2 | 奥力给 |30 |1 | 18000.00 | 2020-01-08 |1 |1 | 研发部 ||3 | 老八|28 |1 | 7000.00| 2020-01-07 |1 |1 | 研发部 ||4 | 小张|25 |1 | 8000.00| 2020-01-10 |1 |1 | 研发部 ||5 | 小红|20 |2 | 6000.00| 2020-01-05 |2 |2 | 人事部 ||6 | 小丽|23 |2 | 6500.00| 2020-01-05 |2 |2 | 人事部 ||7 | 小花|21 |2 | 5500.00| 2020-01-10 |2 |2 | 人事部 ||8 | 马小跳 |25 |1 | 7000.00| 2020-01-01 |3 |3 | 测试部 ||9 | 张大骚 |30 |1 | 9000.00| 2020-01-07 |3 |3 | 测试部 || 10 | 马冬梅 |31 |2 | 5000.00| 2020-01-07 |4 |4 | 销售部 |+----+-----------+-----+-----+----------+------------+---------+-----+-----------+10 rows in set (0.04 sec)其输出结果和多表联合查询一致 。
MySQL多表查询讲解

文章插图
 
如果附加其他条件,可以直接用and连接符连接在on语句的后面
mysql> select * from employee inner join dept on employee.dept_id = dept.did and employee.salary >= 10000; 输出结果
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+| id | name| age | sex | salary| hire_date| dept_id | did | dname|+----+-----------+-----+-----+----------+------------+---------+-----+-----------+|1 | 菜虚鲲 |20 |2 | 10000.00 | 2020-01-10 |1 |1 | 研发部 ||2 | 奥力给 |30 |1 | 18000.00 | 2020-01-08 |1 |1 | 研发部 |+----+-----------+-----+-----+----------+------------+---------+-----+-----------+2 rows in set (0.00 sec)4. Left左外连接查询左外连接查询,即左表的数据全部显示 。
4.1 语法select * from 表1 left join 表2 on [条件];4.2 实战查询员工和部门的所有信息
select * from employee left join dept on employee.dept_id = dept.did;输出结果
mysql> select * from employee left join dept on employee.dept_id = dept.did;+----+-----------+-----+-----+----------+------------+---------+------+-----------+| id | name| age | sex | salary| hire_date| dept_id | did| dname|+----+-----------+-----+-----+----------+------------+---------+------+-----------+|1 | 菜虚鲲 |20 |2 | 10000.00 | 2020-01-10 |1 |1 | 研发部 ||2 | 奥力给 |30 |1 | 18000.00 | 2020-01-08 |1 |1 | 研发部 ||3 | 老八|28 |1 | 7000.00| 2020-01-07 |1 |1 | 研发部 ||4 | 小张|25 |1 | 8000.00| 2020-01-10 |1 |1 | 研发部 ||5 | 小红|20 |2 | 6000.00| 2020-01-05 |2 |2 | 人事部 ||6 | 小丽|23 |2 | 6500.00| 2020-01-05 |2 |2 | 人事部 ||7 | 小花|21 |2 | 5500.00| 2020-01-10 |2 |2 | 人事部 ||8 | 马小跳 |25 |1 | 7000.00| 2020-01-01 |3 |3 | 测试部 ||9 | 张大骚 |30 |1 | 9000.00| 2020-01-07 |3 |3 | 测试部 || 10 | 马冬梅 |31 |2 | 5000.00| 2020-01-07 |4 |4 | 销售部 || 11 | 川坚果 |60 |1 | 100.00| 2020-01-08 | NULL| NULL | NULL|+----+-----------+-----+-----+----------+------------+---------+------+-----------+11 rows in set (0.00 sec)左表中的数据全部显示,右表中的数据只显示符合条件的,不符合条件的以NULL填充


推荐阅读