SQL multi-table query introduction

SQL multi-table query introduction

  • (1) Initialize data
  • (2) In the actual project, the data is stored in multiple tables. If the required data comes from multiple tables, it is necessary to use multi-table query
  • (3) The classification of queries
    "1: Cross join query (understanding)
    "2: Inner join query
    Implicit inner join, explicit inner join***
    "3: Outer join query
    Left outer join, right outer join
    " 4: Sub Inquire
# 初始化数据
create database day13_3;
use database day13_3;

create table category (
  cid int primary key ,
  cname varchar(50)
);
create table products(
  pid int primary key ,
  pname varchar(50),
  price int,
  flag varchar(2),				#是否上架标记为:1表示上架、0表示下架
  category_id int,
  constraint products_fk foreign key (category_id) references category (cid)
);


#分类
insert into category(cid,cname) values('1','家电');
insert into category(cid,cname) values('2','服饰');
insert into category(cid,cname) values('3','化妆品');
#商品
insert into products(pid, pname,price,flag,category_id) values('1','联想',5000,'1',1);
insert into products(pid, pname,price,flag,category_id) values('2','海尔',3000,'1',1);
insert into products(pid, pname,price,flag,category_id) values('3','雷神',5000,'1',1);

insert into products (pid, pname,price,flag,category_id) values('4','杰克琼斯',800,'1',2);
insert into products (pid, pname,price,flag,category_id) values('5','真维斯',200,'1',2);
insert into products (pid, pname,price,flag,category_id) values('6','花花公子',440,'1',2);

insert into products (pid, pname,price,flag,category_id) values('7','劲霸',2000,'1',2);
insert into products (pid, pname,price,flag,category_id) values('8','香奈儿',800,'1',2);
insert into products (pid, pname,price,flag,category_id) values('9','相宜本草',200,'1',2);

Multi-table query-cross join query

(1) What kind of cross join query?
Cross join query is to multiply two tables: A and B —> A * B
(2) What is a Cartesian set?
The result of multiplying two tables, regardless of whether the data is correct
Insert picture description here

(3) The essence of
multi-table query Multi-table query can be understood as conditional filtering on the basis of Cartesian set
(4) Case: query each category and all products in that category

select * from category,products;

Multi-table query-inner join

  • (1) What is inner join query?
    What you seek is the intersection of multiple tables. The
    essence is to add conditions to the Cartesian set.
  • (2) Classification:
    implicit inner join: select * from A, B where condition;
    explicit inner join : select * from A inner join B on condition; (higher efficiency)
    (3) difference
    "using the keyword inner join
    " former Filter on the basis of the Cartesian set, the latter on the original list
    (4) Exercise: Provinces and cities
隐式内连接:select * from A,B where 条件;

select * from category c,products p where c.cid = p.category_id;

显式内连接:select * from A inner join B on 条件;(效率更高)

select * from category c inner	join products p on c.cid = p.category_id;


select * from province,city;
select * from province p inner join city c on p.pid = c.pid_fk;

Multi-table query-outer join

  • (1) 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
  • (2) Classification
    Left outer join
    select * from A left outer join B on 条件;
    is mainly on the left table, all the data in the left table is output, and if there is no equivalent data in the right table, NULL is filled in. The
    right outer join
    select * from A right outer join B on 条件;
    is mainly based on the right table, and all the data in the right table is output. Fill NULL if there is no equivalent data in the table
    Insert picture description here
# 外连接

select * from province p inner join city c on p.pid = c.pid_fk;
# 左外连接
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;

Practice
(1) Query what role each actor plays
(2) Query what role each actor has and what permissions

Subquery

  • (1) What is a subquery?
    Select nesting: the query result of one select is used as part of the syntax of another select query
  • (2) Cases
    Query products belonging to 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='电子')

Query the products belonging to the category of household appliances and cosmetics
in the product table. Query the products belonging to the category of cosmetics in the product table (you will get a table after the select query)

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108759720