[MySQL] Cartesian product - multi-table query (detailed explanation)

When we usually use a database, it is impossible to check only one table. When we want to combine multiple tables for query, we need to perform multi-table query.
Let's design 4 interrelated tables:

insert image description here
Then we now use the Cartesian product to query the scores of z1's analog and digital circuits:

mysql> select * from class,course,score,student where student.studentId = score.studentId and course.courseId = score.courseId
    -> and class.classId = student.studentClassId
    -> and studentName = 'z1'
    -> and course.courseName = '模拟电路与数字电路';
+---------+-----------+----------+--------------------+-----------+----------+-------+-----------+-------------+----------------+
| classId | className | courseId | courseName         | studentId | courseId | score | studentId | studentName | studentClassId |
+---------+-----------+----------+--------------------+-----------+----------+-------+-----------+-------------+----------------+
|       1 | 计算机1|        1 | 模拟电路与数字电路 |         1 |        1 |    78 |         1 | z1          |              1 |
+---------+-----------+----------+--------------------+-----------+----------+-------+-----------+-------------+----------------+
1 row in set (0.00 sec)

insert image description here
The red part in front is the multi-table union. Then we are doing data screening.

Guess you like

Origin blog.csdn.net/weixin_54130714/article/details/123178088