1. What is multi-table connection?
A multi-table join is an SQL operation that combines data from two or more tables together. By joining, we can extract the associated data based on the relationship between tables (such as primary and foreign keys). Connection operation is one of the core functions of relational databases and is widely used in data analysis, report generation and other scenarios.
2. Connection types supported by MySQL
Connection type | effect | Features | Applicable scenarios | MySQL Support | Connection method |
---|---|---|---|---|---|
INNER JOIN |
Returns the matching rows in the two tables. | Return only records that meet the connection conditions. | You need to get exactly matching data in the two tables. | support | Inner connection |
LEFT JOIN |
Returns all rows in the left table, and matching rows in the right table. If there is no matching row in the right table, returnNULL 。 |
The left table is the main one, and the right table is the auxiliary one. | You need to get all the data in the left table, even if there is no matching record in the right table. | support | Left outer connection |
RIGHT JOIN |
Returns all rows in the right table, and matching rows in the left table. If there is no matching row in the left table, returnNULL 。 |
The right table is the main one, and the left table is the auxiliary one. | You need to get all the data in the right table, even if there is no matching record in the left table. | support | Right outer connection |
FULL OUTER JOIN |
Returns all rows in the left and right tables. If a row has no matching row in another table, returnNULL 。 |
Returns the union of two tables. | All data in both tables need to be retrieved, even if there is no matching record. |
Not supported, but can be passedUNION Simulation implementation |
Fully connected |
Notice:
1、OUTER JOIN
is a connection type, which includesLEFT JOIN
、RIGHT JOIN
andFULL OUTER JOIN
. It's withINNER JOIN
different,OUTER JOIN
It will return not only the matching rows, but also the mismatched rows (usingNULL
filling).OUTER JOIN
yesLEFT JOIN
、RIGHT JOIN
andFULL OUTER JOIN
The general term for .
2. By default,JOIN
yesINNER JOIN
abbreviation form of .
3. Syntax of multi-table connection
The basic syntax of multi-table joins is as follows:
SELECT List name FROM surface1 Connection type JOIN surface2 ON surface1.List = surface2.List;
-
Connection type: Can be
INNER JOIN
、LEFT JOIN
、RIGHT JOIN
wait. - ON clause: Specify the connection condition, usually the relationship between the primary key and the foreign key.
4. Practical examples Data preparation
Create two tablesPerson
andAddress
, and insert the test data:
CREATE TABLE Person ( personId INT PRIMARY KEY, firstName VARCHAR(50), lastName VARCHAR(50) ); CREATE TABLE Address ( addressId INT PRIMARY KEY, personId INT, city VARCHAR(50), state VARCHAR(50) ); INSERT INTO Person (personId, firstName, lastName) VALUES (1, 'Allen', 'Wang'), (2, 'Bob', 'Alice'), (3, 'Charlie', 'Brown'); INSERT INTO Address (addressId, personId, city, state) VALUES (1, 2, 'New York City', 'New York'), (2, 3, 'Leetcode', 'California'), (3, 4, 'Mountain View', 'California');
(1) INNER JOIN
Example
returnPerson
andAddress
Matched rows in the table:
SELECT , , , FROM Person p INNER JOIN Address a ON = ;
result:
firstName | lastName | city | state |
---|---|---|---|
Bob | Alice | New York City | New York |
Charlie | Brown | Leetcode | California |
(2) LEFT JOIN
Example
returnPerson
All rows in the table, andAddress
Matched rows in the table:
SELECT , , , FROM Person p LEFT JOIN Address a ON = ;
result:
firstName | lastName | city | state |
---|---|---|---|
Allen | Wang | NULL | NULL |
Bob | Alice | New York City | New York |
Charlie | Brown | Leetcode | California |
(3) RIGHT JOIN
Example
returnAddress
All rows in the table, andPerson
Matched rows in the table:
SELECT , , , FROM Person p RIGHT JOIN Address a ON = ;
result:
firstName | lastName | city | state |
---|---|---|---|
Bob | Alice | New York City | New York |
Charlie | Brown | Leetcode | California |
NULL | NULL | Mountain View | California |
(4) FULL OUTER JOIN
simulation
MySQL does not support itFULL OUTER JOIN
, but can be passedUNION
Simulation implementation:
SELECT , , , FROM Person p LEFT JOIN Address a ON = UNION SELECT , , , FROM Person p RIGHT JOIN Address a ON = ;
result:
firstName | lastName | city | state |
---|---|---|---|
Allen | Wang | NULL | NULL |
Bob | Alice | New York City | New York |
Charlie | Brown | Leetcode | California |
NULL | NULL | Mountain View | California |
5. Connection performance optimization
(1) Use of indexes
In the connection field (e.g.personId
Create indexes on ) can significantly improve query performance:
CREATE INDEX idx_personId ON Address(personId);
Note: The test data in this article creates the primary key, and the index is created by default.(2) Query optimization skills
- Avoid using
SELECT *
, specify the required columns clearly. - use
EXPLAIN
Analyze the query plan and make sure the query uses the index. - Use equal value comparison in connection conditions (
=
), avoid using complex expressions.
This is the article about MySQL multi-table connection (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN) here. For more related content on MySQL multi-table connection, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!