Graphical MySQL inner join, outer join, left join, right join, full join

Use two tables (a_table, b_table), associated fields a_table.a_id and b_table.b_id to demonstrate MySQL's inner join and outer join (left (outer) join, right (outer) join, full (outer) join).

MySQL version: Server version: 5.6.31 MySQL Community Server (GPL)

Database table: a_table, b_table

Topics: inner join, left join (left outer join), right join (right outer join), full join (full outer join)

 

premise

Create table statement:

[sql]  view plain copy  
 
  1. CREATE TABLE `a_table` (  
  2.   `a_id` int(11) DEFAULT NULL,  
  3.   `a_name` varchar(10) DEFAULT NULL,  
  4.   `a_part` varchar(10) DEFAULT NULL  
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8  
[sql]  view plain copy  
 
  1. CREATE TABLE `b_table` (  
  2.   `b_id` int(11) DEFAULT NULL,  
  3.   `b_name` varchar(10) DEFAULT NULL,  
  4.   `b_part` varchar(10) DEFAULT NULL  
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8  

Table test data:

 

1. Internal connection

Keywords: inner join on
语句:select * from a_table a inner join b_table bon a.a_id = b.b_id;
Results of the:

Description: Combine the records in the two tables and return the records whose associated fields match, that is, return the intersection (shaded) part of the two tables.

2. Left join (left outer join)

关键字:left join on / left outer join on
语句:select * from a_table a left join b_table bon a.a_id = b.b_id;
Results of the:
 
illustrate:
Left join is the abbreviation of left outer join, its full name is left outer join, which is a kind of outer join.
For left (outer) joins, the records of the left table (a_table) will all be displayed, while the right table (b_table) will only display records that meet the search criteria. The places where the records in the right table are insufficient are all NULLs.

3. Right join (right outer join)

关键字:right join on / right outer join on
语句:select * from a_table a right outer join b_table b on a.a_id = b.b_id;
Results of the:

illustrate:
Right join is the abbreviation of right outer join. Its full name is right outer join, which is a kind of outer join.
Contrary to the left (outer) join and the right (outer) join, the left table (a_table) will only display the records that meet the search criteria, while the records of the right table (b_table) will all be displayed. Where there are insufficient records in the left table, all are NULL.


Fourth, full connection (full outer connection)

MySQL does not currently support this method, and other methods can be used instead.

5. Supplement, how does MySQL perform associated queries

MySQL considers any query to be an "association", not just a query that needs to match two tables to be called an association, so in MySQL, every query, every segment (including subqueries, and even queries based on a single table) ) can be an association.
The current strategy of MySQL association execution is very simple: MySQL performs a nested loop association operation for any association, that is, MySQL first loops out a single piece of data in one table, and then searches for matching rows in the next table in the nested loop, and then goes on , until all matching rows in the table are found. Then, according to the matching rows of each table, each column required in the query is returned. See the simple query in the following example:
 
Query statement: select tbl1.col1, tbl2.col2 from tbl1 inner join tbl2 using(col3) where tbl1.col1 in (5, 6);
Assuming that MySQL performs the association operations in the order of the tables in the query, we can use the following pseudocode to represent how MySQL will complete the query:
[html]  view plain copy  
 
  1. outer_iter = iterator over tbl1 where col1 in (5, 6)  
  2. outer_row = outer_iter.next  
  3. while outer_row  
  4.     inner_iter = iterator over tbl2 where col3 = outer_row.col3  
  5.     inner_row = inner_iter.next  
  6.     while inner_row  
  7.         output [ outer_row.col1, inner_row.col2]  
  8.         inner_row = inner_iter.next  
  9.     end  
  10.     outer_row = outer_iter.next  
  11. end  
The above execution plan is applicable to both single-table query and multi-table associated query. If it is a single-table query, then only the basic operations of the outer layer above are required. For outer joins, the execution process above still applies. For example, we modify the above query statement as follows:
select tbl1.col1, tbl2.col2 from tbl1 left outer join tbl2 using(col3) where tbl1.col1 in (5, 6);
Then, the corresponding pseudocode is as follows:
[html]  view plain copy  
 
  1. outer_iter = iterator over tbl1 where col1 in (5, 6)  
  2. outer_row = outer_iter.next  
  3. while outer_row  
  4.     inner_iter = iterator over tbl2 where col3 = outer_row.col3  
  5.     inner_row = inner_iter.next  
  6.     if inner_row  
  7.         while inner_row  
  8.             output [ outer_row.col1, inner_row.col2]  
  9.             inner_row = inner_iter.next  
  10.         end  
  11.     else  
  12.         output [ outer_row.col1, null]  
  13.     end  
  14.         outer_row = outer_iter.next  
  15. end  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325577794&siteId=291194637