当前位置:网站首页>[MySQL] internal connection, external connection and self connection (detailed explanation)

[MySQL] internal connection, external connection and self connection (detailed explanation)

2022-06-12 16:39:00 zbossz

First , Internal connection available surface 1 join surface 2on Conditions or surface 1 Alias 1 join surface 2 Alias 2 on Conditions It can also be used. from surface 1, surface 2

Let's start by creating 2 A watch :

 Insert picture description here

Then let's understand the inner connection , External connection :

 Insert picture description here

 Here is the inner connection 
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)
 Below is the left outer connection 
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)
 The following is the right outer connection 
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 look at self connection , The self connected table is itself , That is, self and self combination :
Let's create a table :
 Insert picture 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)
原网站

版权声明
本文为[zbossz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281720471209.html