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版本:Server version: 5.6.31 MySQL Community Server (GPL)
Database tables: 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:
CREATE TABLE `a_table` (
`a_id` int(11) DEFAULT NULL,
`a_name` varchar(10) DEFAULT NULL,
`a_part` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `b_table` (
`b_id` int(11) DEFAULT NULL,
`b_name` varchar(10) DEFAULT NULL,
`b_part` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Table test data:
1. Inner connection
Keywords: inner join on
statement:
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 with the same associated fields, that is, return the intersection (shadow) part of the two tables.
2. Left join (left outer join)
Keyword: left join on / left outer join on
statement:
select * from a_table a left join b_table bon a.a_id = b.b_id;
Results of the:
Explanation:
left join is the abbreviation of left outer join, and its full name is left outer join, which is a kind of outer join.
For left (outer) join, all the records in the left table (a_table) will be displayed, while the right table (b_table) will only display the records that meet the search criteria. The insufficient records in the right table are all NULL.
3. Right join (right outer join)
Keyword: right join on / right outer join on
statement:
select * from a_table a right outer join b_table b on a.a_id = b.b_id;
Results of the:
Explanation:
right join is the abbreviation of right outer join, and its full name is right outer join, which is a kind of outer join.
Contrary to the left (outer) join, the right (outer) join, the left table (a_table) will only display the records that meet the search conditions, and the records in the right table (b_table) will all be displayed. The insufficient records in the left table are all NULL.
Four, full connection (full outer connection)
MySQL does not currently support this method, and other methods can be used instead.
5. Supplement, how does MySQL execute associated queries
MySQL thinks that any query is an "association", not just a query that needs to match two tables to be called an association, so in MySQL, every query, every fragment (including sub-queries, even based on single-table queries ) can be an association.
The current strategy of MySQL association execution is very simple: MySQL executes nested loop association operations for any association, that is, MySQL first loops out a single piece of data in a table, and then searches for matching rows in the next table in the nested loop, and continues in turn , until a matching row is found in all tables. Then, according to the rows matched by each table, each column required in the query is returned. Consider the simple query in the example below:
Check for phrases:
select tbl1.col1, tbl2.col2 from tbl1 inner join tbl2 using(col3) where tbl1.col1 in (5, 6);
Assuming that MySQL performs association operations in the order of the tables in the query, we can use the following pseudocode to express how MySQL will complete this query:
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row = outer_iter.next
while outer_row
inner_iter = iterator over tbl2 where col3 = outer_row.col3
inner_row = inner_iter.next
while inner_row
output [ outer_row.col1, inner_row.col2]
inner_row = inner_iter.next
end
outer_row = outer_iter.next
end
The above execution plan is applicable to both single-table query and multi-table associated query. If it is a single-table query, only the basic operations of the outer layer above are required. For outer joins, the above execution process 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:
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row = outer_iter.next
while outer_row
inner_iter = iterator over tbl2 where col3 = outer_row.col3
inner_row = inner_iter.next
if inner_row
while inner_row
output [ outer_row.col1, inner_row.col2]
inner_row = inner_iter.next
end
else
output [ outer_row.col1, null]
end
outer_row = outer_iter.next
end