MySQL (inner, left, right) join query (multi-table query)

One, MySQL connection query

Grammatical structure of inner join

SELECT column (the field to be queried) FROM table1 (the name of the table that needs to be connected) INNER JOIN (inner connection) table2 ON connection conditions

Note:
1) join: used to query data from two or more tables based on the relationship between the columns in these tables
2) used to query data records that meet certain conditions in two or more tables at the same time
3) syntax Use the INNER JOIN keyword to connect multiple tables, and use ON to set the connection condition
4) is the system default table connection method, you can omit the INNER keyword
5) multiple tables support continuous use of INNERJOIN, it is recommended that no more than three tables
6) ON The latter condition is equal to 1, which is true, otherwise an error will be reported.
7) If the connection condition is a field after ON, the field type must be the same

Insert picture description here

Example operation
  • There are two tables below, the table names and contents are as follows:
第一张表:
mysql> select * from stu;                                                             
+----+---------+-------+------+------+
| id | name    | score | hoby | dizi |
+----+---------+-------+------+------+
|  1 | tianxia | 78.00 |    2 | nj   |
|  2 | diyi    | 89.00 |    1 | nj   |
|  3 | wode    | 55.00 |    1 | NULL |
|  4 | tade    | 57.00 |    3 | NULL |
|  5 | nida    | 66.00 |    1 | nj   |
|  6 | liushou | 55.00 |    3 |      |
|  7 | xiaoer  | 69.00 |   10 | bj   |
|  8 | dage    | 78.00 |    8 | tj   |
+----+---------+-------+------+------+
第二张表:
mysql> select * from hob;
+----+-----------+
| id | hoby     |
+----+-----------+
|  1 | 篮球      |
|  2 | 足球      |
|  3 | 乒乓球    |
|  4 | 羽毛球    |
|  5 | 台球      |
|  6 | 网球      |
+----+-----------+
6 rows in set (0.00 sec)
  • Connect the hoby fields of the two tables through an inner join to query
得到的结果如下:
mysql> select a.id,a.name,b.hooby from stu as a inner join hob as b on a.hoby=b.id;
+----+---------+-----------+
| id | name    | hooby     |
+----+---------+-----------+
|  1 | tianxia | 足球      |
|  2 | diyi    | 篮球      |
|  3 | wode    | 篮球      |
|  4 | tade    | 乒乓球    |
|  5 | nida    | 篮球      |
|  6 | liushou | 乒乓球    |
+----+---------+-----------+
6 rows in set (0.00 sec)

Two, MySQL left join query

Grammatical structure of left connection

SELECT column (the field to be queried) FROM table1 (the name of the table that needs to be joined to query) LEFT JOIN (left join) table2 ON join conditions

Note:
1) Suppose there are two tables A and B. The left join query means that the A table is on the left and the B table is sliding on the right. The A table and the B table are related records through a condition, and the B table matches the A table.
2) Used to query all the contents of a table and the records that meet the conditions of another table, that is, match all the records in the left table and the eligible records in the right table.
3) Use the LEFT JOIN keyword in the syntax to join the table, and use ON sets the connection condition
4) Fields that are not matched are supplemented with NULL by default
Insert picture description here

Example operation
  • Connect the hoby fields of the two tables through the left join query
得到结果如下:
mysql> select a.id,a.name,b.hooby from stu as a left join hob as b on a.hoby=b.id;   
+----+---------+-----------+
| id | name    | hooby     |
+----+---------+-----------+
|  2 | diyi    | 篮球      |
|  3 | wode    | 篮球      |
|  5 | nida    | 篮球      |
|  1 | tianxia | 足球      |
|  4 | tade    | 乒乓球    |
|  6 | liushou | 乒乓球    |
|  7 | xiaoer  | NULL      |
|  8 | dage    | NULL      |
+----+---------+-----------+
8 rows in set (0.00 sec)

Three, MySQL right join query

Grammatical structure of right connection

SELECT column (the field to be queried) FROM table1 (the name of the table to be queried) RIGHT JOIN (left join) table2 ON connection condition
Note:
1) Use the RIGHT JOIN keyword to join the table in the syntax, and use ON to set the connection condition
2 ) Used to query all the contents of one table and the records that meet the conditions of another table, that is, match all the records in the right table and the eligible records in the left table.
3) Unmatched fields are supplemented with NULL by default

Example operation
  • Connect the hoby fields of the two tables through the left join query
mysql> select a.id,a.name,b.hooby from stu as a right join hob as b on a.hoby=b.id;
+------+---------+-----------+
| id   | name    | hooby     |
+------+---------+-----------+
|    1 | tianxia | 足球      |
|    2 | diyi    | 篮球      |
|    3 | wode    | 篮球      |
|    4 | tade    | 乒乓球    |
|    5 | nida    | 篮球      |
|    6 | liushou | 乒乓球    |
| NULL | NULL    | 羽毛球    |
| NULL | NULL    | 台球      |
| NULL | NULL    | 网球      |
+------+---------+-----------+
9 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/wulimingde/article/details/109134307