Mysql: diagram the difference between inner join, left join, right join, full outer join, union, union all

For SQL Join, it may be confusing to learn. We know that SQL Join syntax has many inner, outer, and left. Sometimes, it is not very clear what the result set from Select looks like. There is an article on Coding Horror, through Venn diagram (Venn diagram, can be used to represent the logical relationship between multiple sets). SQL Join is explained. I find it clear and understandable, turn around.

 

Suppose we have two tables. Table A is the table on the left. Table B is the table on the right. Each of them has four records, two of which have the same name, as shown below: Let's see the difference between different JOINs

 

Form A
id name
1 Pirate
2 Monkey
3 Ninja
4 Spaghetti
Form B
id name
1 Rutabaga
2 Pirate
3 Darth Vade
4 Ninja

1.INNER JOIN

SELECT * FROM TableA INNER JOIN TableB ON TableA.name = TableB.name

result set
(TableA.) (TableB.)
id name id name
1 Pirate 2 Pirate
3 Ninja 4 Ninja

The result set produced by Inner join is the intersection of A and B.

Mysql: diagram the difference between inner join, left join, right join, full outer join, union, union all
2.FULL [OUTER] JOIN 
 
(1) SELECT * FROM TableA  FULL OUTER JOIN  TableB ON TableA.name = TableB.name
result set
(TableA.) (TableB.)
id name id name
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vade
Full outer join produces the union of A and B. But it should be noted that for no matching records, null will be used as the value.
You can use IFNULL judgment.
Mysql: diagram the difference between inner join, left join, right join, full outer join, union, union all
 
(2) SELECT * FROM TableA  FULL OUTER JOIN  TableB ON TableA.name = TableB.name
WHERE TableA.id IS null OR TableB.id IS null
result set
(TableA.) (TableB.)
id name id name
2 Monkey null null
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vade
 Generate a dataset in which table A and table B have no intersection.
Mysql: diagram the difference between inner join, left join, right join, full outer join, union, union all
 
3.LEFT [OUTER] JOIN
 
(1) SELECT * FROM TableA  LEFT OUTER JOIN  TableB ON TableA.name = TableB.name
result set
(TableA.) (TableB.)
id name id name
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
Left outer join produces the complete set of table A, and the matching value in table B will be replaced by null value if there is no match.
Mysql: diagram the difference between inner join, left join, right join, full outer join, union, union all
 
(2) SELECT * FROM TableA  LEFT OUTER JOIN  TableB ON TableA.name = TableB.name  WHERE TableB.id IS null
 
result set
(TableA.) (TableB.)
id name id name
2 Monkey null null
4 Spaghetti null null

Produces sets that are in table A but not in table B.

Mysql: diagram the difference between inner join, left join, right join, full outer join, union, union all
 
4.RIGHT [OUTER] JOIN
RIGHT OUTER JOIN is based on the following table, similar to LEFT OUTER JOIN usage. Not introduced here.  
 
5. UNION  and  UNION ALL
The UNION operator is used to combine the result sets of two or more SELECT statements.
Note that the SELECT statement inside the UNION must have the same number of columns. Columns must also have similar data types. Also, the order of the columns in each SELECT statement must be the same. UNION selects only records, while UNION ALL lists all records.
 
(1)SELECT name FROM TableA  UNION  SELECT name FROM TableB
new result set
name
Pirate
Monkey
Ninja
Spaghetti
Rutabaga
Darth Vade
pick a different value
 
(2)SELECT name FROM TableA  UNION ALL  SELECT name FROM TableB
new result set
name
Pirate
Monkey
Ninja
Spaghetti
Rutabaga
Pirate
Darth Vade
Ninja

list all

 

(3) Note:

SELECT * FROM TableA  UNION  SELECT * FROM TableB
new result set
id name
1 Pirate
2 Monkey
3 Ninja
4 Spaghetti
1 Rutabaga
2 Pirate
3 Darth Vade
4 Ninja
Since id 1 Pirate and id 2 Pirate are not the same, do not merge
 
What also needs to be registered is that we also have a "intersection set" cross join. This kind of Join cannot be represented by a textual graph, because it is a combination of N*M of the data of table A and table B, that is, Descartes product. The expression is as follows: SELECT * FROM TableA CROSS JOIN TableB
This Cartesian product yields 4 x 4 = 16 records, and in general, we rarely use this syntax. But we have to be careful, if you don't use nested select statements, the general system will generate a Cartesian product and then do the filtering. This is very dangerous for performance, especially when the table is very large.
 

If there is a where, you can use:

SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...
WHERE where_conditions;

 

 

More references:

PHP5: mysqli Insert, Query, Update and Delete Insert Update Delete Using mysqli (CRUD)

MySQL: Get a MySQL table structure with DESCRIBE

Three things you may need to know about MySQL LEFT JOIN

MySQL temporary table

 

 

This article is transferred from: Mysql: Graphical difference between inner join, left join, right join, full outer join, union, union all

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326654400&siteId=291194637