The connection method in MySQL table is actually very simple, here we simply list their characteristics. Table connections (JOIN) can be divided into inner connections (JOIN/INNER JOIN) and outer connections (LEFT JOIN/RIGHT JOIN). First, let's look at the two tables in this demonstration: mysql> SELECT * FROM student; +------+----------+------+------+ | s_id | s_name | age | c_id | +------+----------+------+------+ | 1 | xiaoming | 13 | 1 | | 2 | xiaohong | 41 | 4 | | 3 | xiaoxia | 22 | 3 | | 4 | xiaogang | 32 | 1 | | 5 | xiaoli | 41 | 2 | | 6 | wangwu | 13 | 2 | | 7 | lisi | 22 | 3 | | 8 | zhangsan | 11 | 9 | +------+----------+------+------+ 8 rows in set (0.00 sec) mysql> SELECT * FROM class; +------+---------+-------+ | c_id | c_name | count | +------+---------+-------+ | 1 | MATH | 65 | | 2 | CHINESE | 70 | | 3 | ENGLISH | 50 | | 4 | HISTORY | 30 | | 5 | BIOLOGY | 40 | +------+---------+-------+ 5 rows in set (0.00 sec) First of all, the prerequisite for tables to be connected is that there are the same comparable columns in the two tables. 1. Inner Join mysql> SELECT * FROM student INNER JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | +------+----------+------+------+------+---------+-------+ 7 rows in set (0.00 sec) Simply put, an inner join is to display all the data of the rows that meet the conditions in the two tables together. That is, if the conditions are not met, the data that can be found in table A but not in B (or vice versa) will not be displayed. 2. Outer Join mysql> SELECT * FROM student LEFT JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | | 8 | zhangsan | 11 | 9 | NULL | NULL | NULL | +------+----------+------+------+------+---------+-------+ 8 rows in set (0.00 sec) mysql> SELECT * FROM student RIGHT JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | NULL | NULL | NULL | NULL | 5 | BIOLOGY | 40 | +------+----------+------+------+------+---------+-------+ 8 rows in set (0.00 sec) The above shows two cases of outer join: left join and right join. These two are almost the same, the only difference is that the main table of the left join is the table on the left, and the main table of the right join is the table on the right. The difference between an outer join and an inner join is that it displays all the rows of the main table, and replaces the data in the main table that is not in other tables with NULL. Summarize This concludes this article about several ways to connect tables in MySQL. For more information about how to connect MySQL tables, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed tutorial on using the tomcat8-maven-plugin plugin in Maven
>>: JavaScript to implement checkbox selection or cancellation
introduction Sometimes, if there are a large numb...
Continuing from the previous article, we will cre...
This article example shares the specific code of ...
Simply put, src means "I want to load this r...
The download address of FlashFXP is: https://www....
Open the centos yum folder Enter the command cd /...
Table of contents 1. How to obtain elements Get i...
This article records the detailed tutorial of MyS...
Tomcat itself optimization Tomcat Memory Optimiza...
Form submission code 1. Source code analysis <...
Download https://tomcat.apache.org/download-80.cg...
This article example shares the specific code of ...
1. Mathematical Functions ABS(x) returns the abso...
Install jdk: Oracle official download https://www...
Table of contents Binding Class Binding inline st...