0
点赞
收藏
分享

微信扫一扫

MySQL中对连接查询的初步理解(内附实例)

芭芭蘑菇 2022-04-13 阅读 56
mysql

下面有两个数据表student02和score02。

请你利用子查询,查询山西省的学生的姓名、出生年份、院系、考试科目及成绩。

mysql> select * from student02;
+--------+----------+-----+-------+------------+--------------+
| stu_id | stu_name | sex | birth | department | addr         |
+--------+----------+-----+-------+------------+--------------+
|    901 | 张飞     | 男  |  1985 | 计算机系   | 河北省涿州市 |
|    902 | 关羽     | 男  |  1986 | 中文系     | 山西省运城市 |
|    903 | 貂蝉     | 女  |  1990 | 中文系     | 山西省忻州市 |
|    904 | 刘备     | 男  |  1990 | 英语系     | 河北省涿州市 |
|    905 | 小乔     | 女  |  1991 | 英语系     | 安徽省潜山市 |
|    906 | 赵云     | 男  |  1988 | 计算机系   | 河北省正定市 |
+--------+----------+-----+-------+------------+--------------+
6 rows in set (0.09 sec)

 

mysql> select * from score02;
+----------+--------+--------+-------+
| score_id | stu_id | c_name | grade |
+----------+--------+--------+-------+
|        1 |    901 | 计算机 |    98 |
|        2 |    901 | 英语   |    80 |
|        3 |    902 | 计算机 |    65 |
|        4 |    902 | 中文   |    88 |
|        5 |    903 | 中文   |    95 |
|        6 |    904 | 计算机 |    70 |
|        7 |    904 | 英语   |    92 |
|        8 |    905 | 英语   |    94 |
|        9 |    906 | 计算机 |    90 |
|       10 |    906 | 英语   |    85 |
+----------+--------+--------+-------+
10 rows in set (0.09 sec)

拿到题目,我的第一反应是这样的:

mysql> select stu_name, birth, department, c_name, grade from student02, score02 where score02.stu_id in (select stu_id from student02 where addr regexp '^山西省');
+----------+-------+------------+--------+-------+
| stu_name | birth | department | c_name | grade |
+----------+-------+------------+--------+-------+
| 张飞     |  1985 | 计算机系   | 计算机 |    65 |
| 关羽     |  1986 | 中文系     | 计算机 |    65 |
| 貂蝉     |  1990 | 中文系     | 计算机 |    65 |
| 刘备     |  1990 | 英语系     | 计算机 |    65 |
| 小乔     |  1991 | 英语系     | 计算机 |    65 |
| 赵云     |  1988 | 计算机系   | 计算机 |    65 |
| 张飞     |  1985 | 计算机系   | 中文   |    88 |
| 关羽     |  1986 | 中文系     | 中文   |    88 |
| 貂蝉     |  1990 | 中文系     | 中文   |    88 |
| 刘备     |  1990 | 英语系     | 中文   |    88 |
| 小乔     |  1991 | 英语系     | 中文   |    88 |
| 赵云     |  1988 | 计算机系   | 中文   |    88 |
| 张飞     |  1985 | 计算机系   | 中文   |    95 |
| 关羽     |  1986 | 中文系     | 中文   |    95 |
| 貂蝉     |  1990 | 中文系     | 中文   |    95 |
| 刘备     |  1990 | 英语系     | 中文   |    95 |
| 小乔     |  1991 | 英语系     | 中文   |    95 |
| 赵云     |  1988 | 计算机系   | 中文   |    95 |
+----------+-------+------------+--------+-------+
18 rows in set (0.12 sec)

结果这个运行结构就让人一脸懵,让我们先检查下子查询的返回值。

mysql> select stu_id from student02 where addr regexp '^山西省';
+--------+
| stu_id |
+--------+
|    902 |
|    903 |
+--------+
2 rows in set (0.10 sec)

没有任何毛病。

但通过仔细观察我们发现上方运行结果是student02表中的每个(全部)stu_name, birth, department值,对应固定的当score02.stu_id = 902或当score02.stu_id = 903的值。

最后我们发现

这样的话,第一次的运行代码,只确定了score02.stu_id的值为903,没有确定student02.stu_id的值,因此打印的是student02.stu_id中的所有值对应score02.stu_id = 903的值。而连接查询中我们最应该做的就是连接两个表格,再进行查询。

正确打开方式如下:

要同时确定score02.stu_id和student02.stu_id的值,再进行条件查询。

mysql> select stu_name, birth, department, c_name, grade from student02, score02 where student02.stu_id = score02.stu_id and score02.stu_id in (select stu_id from student02 where addr regexp '^山西省');
+----------+-------+------------+--------+-------+
| stu_name | birth | department | c_name | grade |
+----------+-------+------------+--------+-------+
| 关羽     |  1986 | 中文系     | 计算机 |    65 |
| 关羽     |  1986 | 中文系     | 中文   |    88 |
| 貂蝉     |  1990 | 中文系     | 中文   |    95 |
+----------+-------+------------+--------+-------+
3 rows in set (0.10 sec)

 

举报

相关推荐

0 条评论