Join usage in MySQL

Join usage of SQL

# 1 Environmental

MySQL5.7
Mac

# 2 Create a table and insert data

New tableA two tables and tableB

mysql> create table tableA ( id int(10), name varchar(100) );
mysql> create table tableB ( id int(10), name varchar(100) );

Insert data

tableA:

mysql> insert into tableA values(1,'布鲁日');
mysql> insert into tableA values(2,'巴黎');
mysql> insert into tableA values(3,'伦敦');
mysql> insert into tableA values(4,'柏林');
mysql> insert into tableA values(5,'耶路撒冷');

tableB:

mysql> insert into tableB values(1,'多哈');
mysql> insert into tableB values(2,'耶路撒冷');
mysql> insert into tableB values(3,'新德里');
mysql> insert into tableB values(4,'马尼拉');
mysql> insert into tableB values(5,'吉隆坡');

20200112132138-image.png

# 3 Start

#3.1 inner join

The result is the intersection of A and B, inner join may be abbreviated to join

select * from tableA join tableB ON tableA.name=tableB.name;

Output:

+------+--------------+------+--------------+
| id   | name         | id   | name         |
+------+--------------+------+--------------+
|    5 | 耶路撒冷     |    2 | 耶路撒冷     |
+------+--------------+------+--------------+

20200112151317-image.png

#3.2 left join

Generating a complete set of Table A, Table B and there is a matching value (substituted places no matching value of null). left join returns all rows left table and the right table rows satisfy ON condition, if no match table left and right rows of the table, then this row of the table with the right place data corresponding NULL.

select * from tableA left join tableB ON tableA.name=tableB.name;

Output:

+------+--------------+------+--------------+
| id   | name         | id   | name         |
+------+--------------+------+--------------+
|    5 | 耶路撒冷     |    2 | 耶路撒冷     |
|    1 | 布鲁日       | NULL | NULL         |
|    2 | 巴黎         | NULL | NULL         |
|    3 | 伦敦         | NULL | NULL         |
|    4 | 柏林         | NULL | NULL         |
+------+--------------+------+--------------+

20200112151410-image.png

Left unique table:

select * from tableA left join tableB ON tableA.name=tableB.name where tableB.name is null;

Output:

+------+-----------+------+------+
| id   | name      | id   | name |
+------+-----------+------+------+
|    1 | 布鲁日    | NULL | NULL |
|    2 | 巴黎      | NULL | NULL |
|    3 | 伦敦      | NULL | NULL |
|    4 | 柏林      | NULL | NULL |
+------+-----------+------+------+

20200112152142-image.png

#3.3 right join

Contrary to join left,Generating a complete set of Table B, and Table A is matched with a value of (substituents places no matching value of null). left join returns all rows left table and the right table rows satisfy ON condition, if no match table left and right rows of the table, then this row of the table with the right place data corresponding NULL.

select * from tableA right join tableB ON tableA.name=tableB.name;

Output:

+------+--------------+------+--------------+
| id   | name         | id   | name         |
+------+--------------+------+--------------+
|    5 | 耶路撒冷     |    2 | 耶路撒冷     |
| NULL | NULL         |    1 | 多哈         |
| NULL | NULL         |    3 | 新德里       |
| NULL | NULL         |    4 | 马尼拉       |
| NULL | NULL         |    5 | 吉隆坡       |
+------+--------------+------+--------------+

20200112151501-image.png

Exclusive right table:

select * from tableA right join tableB ON tableA.name=tableB.name where tableA.name is null;

Output:

+------+------+------+-----------+
| id   | name | id   | name      |
+------+------+------+-----------+
| NULL | NULL |    1 | 多哈      |
| NULL | NULL |    3 | 新德里    |
| NULL | NULL |    4 | 马尼拉    |
| NULL | NULL |    5 | 吉隆坡    |
+------+------+------+-----------+

20200112152345-image.png

#3.4 full join

And A and B are set, oracle which has full join, butNo full join in mysql. We can use the union to achieve their goals.

select * from tableA left join tableB ON tableA.name=tableB.name 
union
select * from tableA right join tableB ON tableA.name=tableB.name;

Output:

+------+--------------+------+--------------+
| id   | name         | id   | name         |
+------+--------------+------+--------------+
|    5 | 耶路撒冷     |    2 | 耶路撒冷     |
|    1 | 布鲁日       | NULL | NULL         |
|    2 | 巴黎         | NULL | NULL         |
|    3 | 伦敦         | NULL | NULL         |
|    4 | 柏林         | NULL | NULL         |
| NULL | NULL         |    1 | 多哈         |
| NULL | NULL         |    3 | 新德里       |
| NULL | NULL         |    4 | 马尼拉       |
| NULL | NULL         |    5 | 吉隆坡       |
+------+--------------+------+--------------+

20200112151827-image.png

Published 110 original articles · won praise 20 · views 40000 +

Guess you like

Origin blog.csdn.net/Coxhuang/article/details/103946252