当前位置:网站首页>[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 :

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

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 :
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)
边栏推荐
猜你喜欢

Cookie 和 Session

武汉大学甘菲课题组和南昌大学徐振江课题组联合招聘启事

The C programming language (version 2) notes / 8 UNIX system interface / 8.7 instance (storage allocator)

acwing 801. Number of 1 in binary (bit operation)

Super detailed dry goods! Docker+pxc+haproxy build a MySQL Cluster with high availability and strong consistency

Acwing 798 two dimensional difference (difference matrix)

generate pivot data 0

有哪些特容易考上的院校?

Leetcode 2190. 数组中紧跟 key 之后出现最频繁的数字(可以,一次过)

ISCC-2022 部分wp
随机推荐
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.5 实例(fopen 和 getc 函数的实现)
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.3 open、creat、close、unlink
MySQL面试整理
ISCC-2022 部分wp
What's the matter with pbootcms' if judgment failure and direct display of labels?
Acwing794 high precision Division
token与幂等性问题
QCustomplot笔记(一)之QCustomplot添加数据以及曲线
How to base on CCS_ V11 new tms320f28035 project
数据库的三大范式
Token and idempotency
<山东大学项目实训>渲染引擎系统(三)
Sum of acwing796 submatrix
The C programming language (version 2) notes / 8 UNIX system interface / 8.7 instance (storage allocator)
Canvas image processing (Part 1)
【研究】英文论文阅读——英语poor的研究人员的福利
Iscc-2022 part WP
Analysis of Nacos config dynamic refresh source code
std::set compare
有哪些特容易考上的院校?