MySQL多表联合查询
文章目录
1. 什么是多表联合查询
一般查询语句都是针对一个表的,但是在关系型数据库中,表与表之间是有联系的,所以在实际应用中,经常使用多表查询。多表查询就是同时查询两个或两个以上的表
在 MySQL 中,多表查询主要有交叉连接、内连接、外连接、分组查询与子查询等5种
2. 交叉连接
交叉连接(CROSS JOIN):有两种,显式的和隐式的2种,一般用来返回连接表的笛卡尔积。
笛卡尔积(Cartesian product)是指两个集合 X 和 Y 的乘积
多个表交叉连接时,在 FROM 后表之间连续使用 CROSS JOIN 或逗号即可,两种语法的返回结果是相同的
表的数量较多时,交叉连接会非常非常慢。一般情况下不建议使用交叉连接
在 MySQL 中,多表查询一般使用内连接和外连接,它们的效率要高于交叉连接
使用 CROSS JOIN 查询出两张表中的笛卡尔积:
mysql> create database school;
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> use school
Database changed
mysql> create table tb_students_info(id int not null primary key auto_increment,name varchar(30),age tinyint,sex varchar(6),height int,course_id int);
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> insert tb_students_info(name,age,sex,height,course_id) values('Dany',25,'M',160,1),('Green',23,'M',158,2),('Henry',23,'F',185,1),('Jane',22,'M',162,3),('Jim',24,'F',175,2),('John',21,'F',172,4),,('Lily',22,'M',165,4),('Susan',23,'M',170,5),('Thomas',22,'F',178,5),('Tom',23,'F',165,5);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | Dany | 25 | M | 160 | 1 |
| 2 | Dany | 25 | M | 160 | 1 |
| 3 | Green | 23 | M | 158 | 2 |
| 4 | Henry | 23 | F | 185 | 1 |
| 5 | Jane | 22 | M | 162 | 3 |
| 6 | Jim | 24 | F | 175 | 2 |
| 7 | John | 21 | F | 172 | 4 |
| 8 | Lily | 22 | M | 165 | 4 |
| 9 | Susan | 23 | M | 170 | 5 |
| 10 | Thomas | 22 | F | 178 | 5 |
| 11 | Tom | 23 | F | 165 | 5 |
+----+--------+------+------+--------+-----------+
11 rows in set (0.00 sec)
mysql> create table tb_course(id int not null primary key auto_increment,course_name varchar(30));
Query OK, 0 rows affected (0.01 sec)
mysql> insert tb_course(course_name) values('Java'),('MySQL'),('Python'),('Go'),('C++');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | Java |
| 2 | MySQL |
| 3 | Python |
| 4 | Go |
| 5 | C++ |
+----+-------------+
5 rows in set (0.00 sec)
mysql> select * From tb_course cross join tb_students_info;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | M | 160 | 1 |
| 2 | MySQL | 1 | Dany | 25 | M | 160 | 1 |
| 3 | Python | 1 | Dany | 25 | M | 160 | 1 |
| 4 | Go | 1 | Dany | 25 | M | 160 | 1 |
| 5 | C++ | 1 | Dany | 25 | M | 160 | 1 |
| 1 | Java | 2 | Dany | 25 | M | 160 | 1 |
| 2 | MySQL | 2 | Dany | 25 | M | 160 | 1 |
| 3 | Python | 2 | Dany | 25 | M | 160 | 1 |
| 4 | Go | 2 | Dany | 25 | M | 160 | 1 |
| 5 | C++ | 2 | Dany | 25 | M | 160 | 1 |
| 1 | Java | 3 | Green | 23 | M | 158 | 2 |
| 2 | MySQL | 3 | Green | 23 | M | 158 | 2 |
| 3 | Python | 3 | Green | 23 | M | 158 | 2 |
| 4 | Go | 3 | Green | 23 | M | 158 | 2 |
| 5 | C++ | 3 | Green | 23 | M | 158 | 2 |
| 1 | Java | 4 | Henry | 23 | F | 185 | 1 |
| 2 | MySQL | 4 | Henry | 23 | F | 185 | 1 |
| 3 | Python | 4 | Henry | 23 | F | 185 | 1 |
| 4 | Go | 4 | Henry | 23 | F | 185 | 1 |
| 5 | C++ | 4 | Henry | 23 | F | 185 | 1 |
| 1 | Java | 5 | Jane | 22 | M | 162 | 3 |
| 2 | MySQL | 5 | Jane | 22 | M | 162 | 3 |
| 3 | Python | 5 | Jane | 22 | M | 162 | 3 |
| 4 | Go | 5 | Jane | 22 | M | 162 | 3 |
| 5 | C++ | 5 | Jane | 22 | M | 162 | 3 |
| 1 | Java | 6 | Jim | 24 | F | 175 | 2 |
| 2 | MySQL | 6 | Jim | 24 | F | 175 | 2 |
| 3 | Python | 6 | Jim | 24 | F | 175 | 2 |
| 4 | Go | 6 | Jim | 24 | F | 175 | 2 |
| 5 | C++ | 6 | Jim | 24 | F | 175 | 2 |
| 1 | Java | 7 | John | 21 | F | 172 | 4 |
| 2 | MySQL | 7 | John | 21 | F | 172 | 4 |
| 3 | Python | 7 | John | 21 | F | 172 | 4 |
| 4 | Go | 7 | John | 21 | F | 172 | 4 |
| 5 | C++ | 7 | John | 21 | F | 172 | 4 |
| 1 | Java | 8 | Lily | 22 | M | 165 | 4 |
| 2 | MySQL | 8 | Lily | 22 | M | 165 | 4 |
| 3 | Python | 8 | Lily | 22 | M | 165 | 4 |
| 4 | Go | 8 | Lily | 22 | M | 165 | 4 |
| 5 | C++ | 8 | Lily | 22 | M | 165 | 4 |
| 1 | Java | 9 | Susan | 23 | M | 170 | 5 |
| 2 | MySQL | 9 | Susan | 23 | M | 170 | 5 |
| 3 | Python | 9 | Susan | 23 | M | 170 | 5 |
| 4 | Go | 9 | Susan | 23 | M | 170 | 5 |
| 5 | C++ | 9 | Susan | 23 | M | 170 | 5 |
| 1 | Java | 10 | Thomas | 22 | F | 178 | 5 |
| 2 | MySQL | 10 | Thomas | 22 | F | 178 | 5 |
| 3 | Python | 10 | Thomas | 22 | F | 178 | 5 |
| 4 | Go | 10 | Thomas | 22 | F | 178 | 5 |
| 5 | C++ | 10 | Thomas | 22 | F | 178 | 5 |
| 1 | Java | 11 | Tom | 23 | F | 165 | 5 |
| 2 | MySQL | 11 | Tom | 23 | F | 165 | 5 |
| 3 | Python | 11 | Tom | 23 | F | 165 | 5 |
| 4 | Go | 11 | Tom | 23 | F | 165 | 5 |
| 5 | C++ | 11 | Tom | 23 | F | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
55 rows in set (0.00 sec)
查询 tb_course 表中的 id 字段和 tb_students_info 表中的 course_id 字段相等的内容
mysql> select * from tb_course cross join tb_students_info
-> where tb_students_info.course_id = tb_course.id;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | M | 160 | 1 |
| 1 | Java | 2 | Dany | 25 | M | 160 | 1 |
| 2 | MySQL | 3 | Green | 23 | M | 158 | 2 |
| 1 | Java | 4 | Henry | 23 | F | 185 | 1 |
| 3 | Python | 5 | Jane | 22 | M | 162 | 3 |
| 2 | MySQL | 6 | Jim | 24 | F | 175 | 2 |
| 4 | Go | 7 | John | 21 | F | 172 | 4 |
| 4 | Go | 8 | Lily | 22 | M | 165 | 4 |
| 5 | C++ | 9 | Susan | 23 | M | 170 | 5 |
| 5 | C++ | 10 | Thomas | 22 | F | 178 | 5 |
| 5 | C++ | 11 | Tom | 23 | F | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
11 rows in set (0.00 sec)
3. 内连接
内连接(INNER JOIN)主要通过设置连接条件的方式,来移除查询结果中某些数据行的交叉连接。简单来说,就是利用条件表达式来消除交叉连接的某些数据行。
内连接使用 INNER JOIN 关键字连接两张表,并使用 ON 子句来设置连接条件。如果没有连接条件,INNER JOIN 和 CROSS JOIN 在语法上是等同的,两者可以互换。INNER JOIN 也可以使用 WHERE 子句指定连接条件
内连接中可以省略 INNER 关键字,只用关键字 JOIN
在 tb_students_info 表和 tb_course 表之间,使用内连接查询学生姓名和相对应的课程名称:
mysql> select s.name,c.course_name from tb_students_info s inner join tb_course c on s.course_id = c.id;
+--------+-------------+
| name | course_name |
+--------+-------------+
| Dany | Java |
| Dany | Java |
| Green | MySQL |
| Henry | Java |
| Jane | Python |
| Jim | MySQL |
| John | Go |
| Lily | Go |
| Susan | C++ |
| Thomas | C++ |
| Tom | C++ |
+--------+-------------+
11 rows in set (0.00 sec)
4. 外连接
内连接的查询结果都是符合连接条件的记录,而外连接会先将连接的表分为基表和参考表,再以基表为依据返回满足和不满足条件的记录。
外连接可以分为左外连接和右外连接2种
4.1 左连接
左外连接又称为左连接,使用 LEFT OUTER JOIN 关键字连接两个表,并使用 ON 子句来设置连接条件
左连接中可以省略 OUTER 关键字,只使用关键字 LEFT JOIN
在 tb_students_info 表和 tb_course 表中查询所有学生姓名和相对应的课程名称,包括没有课程的学生:
mysql> insert tb_students_info(name,age,sex,height,course_id) values('LiMing',22,'M',180,7);
Query OK, 1 row affected (0.00 sec)
mysql> insert tb_course(course_name) values('HTML');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | Java |
| 2 | MySQL |
| 3 | Python |
| 4 | Go |
| 5 | C++ |
| 6 | HTML |
+----+-------------+
6 rows in set (0.00 sec)
mysql>
mysql> select * from tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | Dany | 25 | M | 160 | 1 |
| 2 | Dany | 25 | M | 160 | 1 |
| 3 | Green | 23 | M | 158 | 2 |
| 4 | Henry | 23 | F | 185 | 1 |
| 5 | Jane | 22 | M | 162 | 3 |
| 6 | Jim | 24 | F | 175 | 2 |
| 7 | John | 21 | F | 172 | 4 |
| 8 | Lily | 22 | M | 165 | 4 |
| 9 | Susan | 23 | M | 170 | 5 |
| 10 | Thomas | 22 | F | 178 | 5 |
| 11 | Tom | 23 | F | 165 | 5 |
| 12 | LiMing | 22 | M | 180 | 7 |
+----+--------+------+------+--------+-----------+
12 rows in set (0.00 sec)
mysql> select s.name,c.course_name from tb_students_info s left outer join tb_course c on s.course_id = c.id;
+--------+-------------+
| name | course_name |
+--------+-------------+
| Dany | Java |
| Dany | Java |
| Green | MySQL |
| Henry | Java |
| Jane | Python |
| Jim | MySQL |
| John | Go |
| Lily | Go |
| Susan | C++ |
| Thomas | C++ |
| Tom | C++ |
| LiMing | NULL |
+--------+-------------+
12 rows in set (0.00 sec)
4.2 右连接
右外连接又称为右连接,右连接是左连接的反向连接。使用 RIGHT OUTER JOIN 关键字连接两个表,并使用 ON 子句来设置连接条件
在 tb_students_info 表和 tb_course 表中查询所有课程,包括没有学生的课程:
mysql> select s.name,c.course_name from tb_students_info s right outer join tb_course c on s.course_iid = c.id;
+--------+-------------+
| name | course_name |
+--------+-------------+
| Dany | Java |
| Green | MySQL |
| Henry | Java |
| Jane | Python |
| Jim | MySQL |
| John | Go |
| Lily | Go |
| Susan | C++ |
| Thomas | C++ |
| Tom | C++ |
| NULL | HTML |
+--------+-------------+
11 rows in set (0.00 sec)
5. 分组查询
在 MySQL 中,GROUP BY 关键字可以根据一个或多个字段对查询结果进行分组
GROUP BY 关键字可以和 GROUP_CONCAT() 函数一起使用。GROUP_CONCAT() 函数会把每个分组的字段值都显示出来
下面根据 tb_students_info 表中的 sex 字段进行分组查询,使用 GROUP_CONCAT() 函数将每个分组的 name 字段的值都显示出来
mysql> select sex,group_concat(name) from tb_students_info group by sex;
+------+-----------------------------------+
| sex | group_concat(name) |
+------+-----------------------------------+
| F | Henry,Jim,John,Thomas,Tom |
| M | Dany,Green,Jane,Lily,Susan,LiMing |
+------+-----------------------------------+
2 rows in set (0.00 sec)
下面根据 tb_students_info 表中的 age 和 sex 字段进行分组查询
mysql> select age,sex,group_concat(name) from tb_students_info
-> group by age,sex;
+------+------+--------------------+
| age | sex | group_concat(name) |
+------+------+--------------------+
| 21 | F | John |
| 22 | F | Thomas |
| 22 | M | Jane,Lily,LiMing |
| 23 | F | Henry,Tom |
| 23 | M | Green,Susan |
| 24 | F | Jim |
| 25 | M | Dany |
+------+------+--------------------+
7 rows in set (0.00 sec)
5.1 GROUP BY 与聚合函数
在数据统计时,GROUP BY 关键字经常和聚合函数一起使用。
聚合函数包括 COUNT(),SUM(),AVG(),MAX() 和 MIN()。其中,COUNT() 用来统计记录的条数;SUM() 用来计算字段值的总和;AVG() 用来计算字段值的平均值;MAX() 用来查询字段的最大值;MIN() 用来查询字段的最小值
使用 COUNT() 函数计算每一组的记录数:
mysql> select sex,count(sex) from tb_students_info
-> group by sex;
+------+------------+
| sex | count(sex) |
+------+------------+
| F | 5 |
| M | 6 |
+------+------------+
2 rows in set (0.00 sec)
5.2 GROUP BY 与 WITH ROLLUP
WITH POLLUP 关键字用来在所有记录的最后加上一条记录,这条记录是上面所有记录的总和,即统计记录数量
mysql> select sex,group_concat(name) from tb_students_info
-> group by sex with rollup;
+------+-------------------------------------------------------------+
| sex | group_concat(name) |
+------+-------------------------------------------------------------+
| F | Henry,Jim,John,Thomas,Tom |
| M | Dany,Green,Jane,Lily,Susan,LiMing |
| NULL | Henry,Jim,John,Thomas,Tom,Dany,Green,Jane,Lily,Susan,LiMing |
+------+-------------------------------------------------------------+
3 rows in set (0.00 sec)
6. 子查询
子查询是 MySQL 中比较常用的查询方法,通过子查询可以实现多表查询。子查询指将一个查询语句嵌套在另一个查询语句中。子查询可以在 SELECT、UPDATE 和 DELETE 语句中使用,而且可以进行多层嵌套。在实际开发时,子查询经常出现在 WHERE 子句中
一般来说,表连接(内连接和外连接等)都可以用子查询替换,但反过来却不一定,有的子查询不能用表连接来替换。子查询比较灵活、方便、形式多样,适合作为查询的筛选条件,而表连接更适合于查看连接表的数据
使用子查询在 tb_students_info 表和 tb_course 表中查询学习 Java 课程的学生姓名
mysql> select name from tb_students_info
-> where course_id in (select id from tb_course where course_name = 'Java');
+-------+
| name |
+-------+
| Dany |
| Henry |
+-------+
2 rows in set (0.00 sec)
使用=运算符,在 tb_course 表和 tb_students_info 表中查询出所有学习 Python 课程的学生姓名
mysql> select name from tb_students_info
-> where course_id = (select id from tb_course where course_name = 'Python');
+------+
| name |
+------+
| Jane |
+------+
1 row in set (0.00 sec)
使用<>运算符,在 tb_course 表和 tb_students_info 表中查询出没有学习 Python 课程的学生姓名
mysql> select name from tb_students_info
-> where course_id <> (select id from tb_course where course_name = 'Python');
+--------+
| name |
+--------+
| Dany |
| Green |
| Henry |
| Jim |
| John |
| Lily |
| Susan |
| Thomas |
| Tom |
| LiMing |
+--------+
10 rows in set (0.00 sec)
查询 tb_course 表中是否存在 id=1 的课程,如果存在,就查询出 tb_students_info 表中的记录
mysql> select * from tb_students_info
-> where exists(select course_name from tb_course where id=1);
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 2 | Dany | 25 | M | 160 | 1 |
| 3 | Green | 23 | M | 158 | 2 |
| 4 | Henry | 23 | F | 185 | 1 |
| 5 | Jane | 22 | M | 162 | 3 |
| 6 | Jim | 24 | F | 175 | 2 |
| 7 | John | 21 | F | 172 | 4 |
| 8 | Lily | 22 | M | 165 | 4 |
| 9 | Susan | 23 | M | 170 | 5 |
| 10 | Thomas | 22 | F | 178 | 5 |
| 11 | Tom | 23 | F | 165 | 5 |
| 12 | LiMing | 22 | M | 180 | 7 |
+----+--------+------+------+--------+-----------+
11 rows in set (0.00 sec)
查询 tb_course 表中是否存在 id=1 的课程,如果存在,就查询出 tb_students_info 表中 age 字段大于 24 的记录
mysql> select * from tb_students_info
-> where age>24 and exists(select course_name from tb_course where id=1);
+----+------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+------+------+------+--------+-----------+
| 2 | Dany | 25 | M | 160 | 1 |
+----+------+------+------+--------+-----------+
1 row in set (0.00 sec)