Introduction to multi-table query
- Initialization data
- In the actual project, the data is stored in multiple tables. If the required data comes from multiple tables, you must use multi-table query
- Type of query
- Cross-connect query (understand)
- Inner join query
Implicit inner join, explicit inner join*** - Outer join query
Left outer join, right outer join - Subquery
#创建数据库
create database web02;
#使用数据库
use web02;
#省表
create table province (
pid int primary key auto_increment,
pname varchar(20)
);
#市表
create table city(
cid int primary key auto_increment,
cname varchar(20)
);
#添加外键字段
alter table city add pid_fk int;
#添加外键约束
alter table city add foreign key(pid_fk) references province(pid);
#省表添加数据
insert into province values(1,'湖南'),(2,'广西');
#市表添加数据
insert into city values(1,'长沙',1),(2,'张家界',1),(3,'桂林',2),(4,'贵港',2);
1. Cross-connect query (understand)
-
What cross-join query?
Cross-join query is to multiply two tables: A and B —> A * B -
What is a Cartesian set?
The result of multiplying two tables, regardless of whether the data is correct
-
The essence of
multi-table query Multi-table query can be understood as conditional filtering on the basis of Cartesian set -
Case: Query each province and all cities in the province
select * from province,city;
2. Inner connection (keyword inner can be omitted)
- What is inner join query?
What you seek is the intersection of multiple tables. The
essence is to add conditions to the Cartesian set. - Classification
Implicit inner join: select * from A, B where condition;
explicit inner join : select * from A inner join B on condition; (more efficient)
#隐式内连接
select * from province p,city c where p.pid = c.pid_fk;
#显式内连接
select * from province p inner join city c on p.pid = c.pid_fk;
3. Difference
Use the keyword inner join
to filter the former on the basis of Cartesian set, and the latter to filter on the original table
3. Outer connection (the keyword outer can be omitted)
- What is an outer join query?
Select one of the two tables to output all the data, and the other table will output NULL if there is no corresponding data. - classification
- Left outer join
select * from A left outer join B on conditions;
the left table is the main one, all the data in the left table is output, and if there is no equivalent data in the right table, NULL is added - Right outer join
select * from A right outer join B on conditions;
the right table is the main one, all the data in the right table is output, and if there is no equivalent data in the left table, NULL is added
- Exercise
#在省表添加一行数据
insert into province values (3,'广东');
#左外连接
select * from province p left outer join city c on p.pid = c.pid_fk;
#右外连接
select * from province p right outer join city c on p.pid = c.pid_fk;
Four, subquery
- What is a subquery?
Select nesting: The result of a select query is used as part of the syntax of another select query. - Case
Query the products belonging to the electronic classification in the product table
select * from product where cid = 1;
select cid from category where cname='电子';
#以上两句结合成一句
select * from product where cid = (select cid from category where cname='电子');