1. Self-join query
The left table and the right table are the same table, and the data in the two tables are queried according to the join query condition.
Regional table renderings
Example 1: Query all cities where the name of the province is "Shanxi Province"
Create the areas table:
create table areas(
id varchar(30) not null primary key,
title varchar(30),
pid varchar(30)
);
Execute the sql file to import data to the areas table:
source areas.sql;
Description:
- source represents the executed sql file
Usage of self-join query:
select c.id, c.title, c.pid, p.title from areas as c inner join areas as p on c.pid = p.id where p.title = '山西省';
Description:
- Since the join query must alias the table
Self-join query
summary
- The self-join query is to simulate a table into two tables on the left and right, and then perform a joined table query.
- Self-connection is a special connection method, the connected table is the table itself