[MySQL] inner join, outer join, self join (detailed explanation)

First, inner joins can be used 表1 join 表2on 条件or 表1 别名1 join 表2 别名2 on 条件can also be usedfrom表1,表2

Let's first create 2 tables:

insert image description here

Then let's understand the inner connection and outer connection:

insert image description here

下面是内连接
mysql> select student.name,class.className from student join class on student.classId = class.classId;
+------+-----------+
| name | className |
+------+-----------+
| z1   | c1        |
| z2   | c2        |
| z3   | c3        |
+------+-----------+
3 rows in set (0.00 sec)
mysql> select stu.name,cla.className from student stu join class cla on stu.classId = cla.classId;
+------+-----------+
| name | className |
+------+-----------+
| z1   | c1        |
| z2   | c2        |
| z3   | c3        |
+------+-----------+
3 rows in set (0.00 sec)
下面是左外连接
mysql> select student.name,class.className from student left join class on student.classId = class.classId;
+------+-----------+
| name | className |
+------+-----------+
| z1   | c1        |
| z2   | c2        |
| z3   | c3        |
| z4   | NULL      |
| z5   | NULL      |
+------+-----------+
5 rows in set (0.00 sec)
mysql> select stu.name,cla.className from student stu left join class cla on stu.classId = cla.classId;
+------+-----------+
| name | className |
+------+-----------+
| z1   | c1        |
| z2   | c2        |
| z3   | c3        |
| z4   | NULL      |
| z5   | NULL      |
+------+-----------+
5 rows in set (0.00 sec)
下面是右外连接
mysql> select student.name,class.className from student right join class on student.classId = class.classId;
+------+-----------+
| name | className |
+------+-----------+
| z1   | c1        |
| z2   | c2        |
| z3   | c3        |
| NULL | c99       |
| NULL | c88       |
+------+-----------+
5 rows in set (0.00 sec)
mysql> select stu.name,cla.className from student stu right join class cla on stu.classId = cla.classId;
+------+-----------+
| name | className |
+------+-----------+
| z1   | c1        |
| z2   | c2        |
| z3   | c3        |
| NULL | c99       |
| NULL | c88       |
+------+-----------+
5 rows in set (0.00 sec)

Next, let's take a look at self-joining. The self-joining table is itself, that is, a combination of itself and itself:
let's create a table:
insert image description here

mysql> select * from studentscore as ss1,studentscore as ss2
    -> where ss1.studentId = ss2.studentId
    -> and ss1.courseId = 1
    -> and ss2.courseId = 3
    -> and ss1.score< ss2.score;
+-----------+-------------+----------+------------+-------+-----------+-------------+----------+------------+-------+
| studentId | studentName | courseId | courseName | score | studentId | studentName | courseId | courseName | score |
+-----------+-------------+----------+------------+-------+-----------+-------------+----------+------------+-------+
|         3 | z3          |        1 | chinese    |    45 |         3 | z3          |        3 | english    |    89 |
+-----------+-------------+----------+------------+-------+-----------+-------------+----------+------------+-------+
1 row in set (0.00 sec)

Guess you like

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