当前位置:网站首页>MySQL - multi table query and case explanation
MySQL - multi table query and case explanation
2022-07-26 19:00:00 【NO.-LL】
Catalog
Overview of multi table query
classification
Internal connection : It is equivalent to a query A、B Intersection part dataExternal connection : There are two types of external connections , Namely : The left outer join and Right connectionThe left outer join : Query all data in the left table , And the intersection of two tablesRight connection : Query all data in the right table , And the intersection of two tablesSelf join : Query the connection between the current table and itself , Self join must use table alias
Data preparation
drop table dept;
drop table emp;
-- establish dept surface , And insert data
create table dept(
id int auto_increment comment 'ID' primary key,
name varchar(50) not null comment ' Department name '
)comment ' Departmental table ';
INSERT INTO dept (id, name) VALUES (1, ' R & D department '), (2, ' The Marketing Department '),(3, ' Finance Department '), (4,
' The sales department '), (5, ' General manager office '), (6, ' The personnel department ');
-- establish emp surface , And insert data
create table emp(
id int auto_increment comment 'ID' primary key,
name varchar(50) not null comment ' full name ',
age int comment ' Age ',
job varchar(20) comment ' Position ',
salary int comment ' Salary ',
entrydate date comment ' Entry time ',
managerid int comment ' Direct leadership ID',
dept_id int comment ' department ID'
)comment ' The employee table ';
-- Add foreign keys
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references
dept(id);
INSERT INTO emp (id, name, age, job,salary, entrydate, managerid, dept_id)
VALUES
(1, ' Jin yong ', 66, ' President ',20000, '2000-01-01', null,5),
(2, ' zhang wuji ', 20, ' project manager ',12500, '2005-12-05', 1,1),
(3, ' Yang Xiao ', 33, ' Development ', 8400,'2000-11-03', 2,1),
(4, ' Xiangr ', 48, ' Development ',11000, '2002-02-05', 2,1),
(5, ' chang yuchun ', 43, ' Development ',10500, '2004-09-07', 3,1),
(6, ' Small zhao ', 19, ' Programmers encourage teachers to ',6600, '2004-10-12', 2,1),
(7, ' extinction ', 60, ' Chief financial officer ',8500, '2002-09-12', 1,3),
(8, ' Zhou Zhiruo ', 19, ' accounting ',48000, '2006-06-02', 7,3),
(9, ' Ding Minjun ', 23, ' Cashier ',5250, '2009-05-13', 7,3),
(10, ' Zhao Min ', 20, ' Director of marketing department ',12500, '2004-10-12', 1,2),
(11, ' Deer stick guest ', 56, ' staff member ',3750, '2006-10-03', 10,2),
(12, ' Crane pen Weng ', 19, ' staff member ',3750, '2007-05-09', 10,2),
(13, ' Fang Dongbai ', 19, ' staff member ',5500, '2009-02-12', 10,2),
(14, ' Zhang Sanfeng ', 88, ' Sales Director ',14000, '2004-10-12', 1,4),
(15, ' Yu Lianzhou ', 38, ' sales ',4600, '2004-10-12', 14,4),
(16, ' Song Yuanqiao ', 40, ' sales ',4600, '2004-10-12', 14,4),
(17, ' Chen Youlang ', 42, null,2000, '2011-10-12', 1,null);Internal connection
SELECT Field list FROM surface 1 , surface 2 WHERE Conditions ... ;
SELECT Field list FROM surface 1 [ INNER ] JOIN surface 2 ON Connection condition ... ;
select emp.name , dept.name from emp , dept where emp.dept_id = dept.id ;
-- Alias each table , simplify SQL To write
select e.name,d.name from emp e , dept d where e.dept_id = d.id;select e.name, d.name from emp e inner join dept d on e.dept_id = d.id;
-- Alias each table , simplify SQL To write
select e.name, d.name from emp e join dept d on e.dept_id = d.id;Be careful : Once the table is aliased , You can no longer use the table name to specify the corresponding field , At this point, only aliases can be used to specify fields
External connection
1). The left outer join
SELECT Field list FROM surface 1 LEFT [ OUTER ] JOIN surface 2 ON Conditions ... ;
2). Right connection
SELECT Field list FROM surface 1 RIGHT [ OUTER ] JOIN surface 2 ON Conditions ... ;
select e.*, d.name from emp e left outer join dept d on e.dept_id = d.id;
select e.*, d.name from emp e left join dept d on e.dept_id = d.id;select d.*, e.* from emp e right outer join dept d on e.dept_id = d.id;
select d.*, e.* from dept d left outer join emp e on e.dept_id = d.id;Self join
SELECT Field list FROM surface A Alias A JOIN surface A Alias B ON Conditions ... ;
select a.name , b.name from emp a , emp b where a.managerid = b.id;select a.name ' staff ', b.name ' Leader ' from emp a left join emp b on a.managerid = b.id;The joint query
SELECT Field list FROM surface A ...UNION [ ALL ]SELECT Field list FROM surface B ....;
- The number of columns in multiple tables of joint query must be consistent , Field types also need to be consistent .
- union all All the data will be merged directly ,union The merged data will be de duplicated
select * from emp where salary < 5000
union all
select * from emp where age > 50;select * from emp where salary < 5000
union
select * from emp where age > 50;Subquery
SELECT * FROM t1 WHERE column1 = ( SELECT column1 FROM t2 );
Scalar subquery
select * from emp where dept_id = (select id from dept where name = ' The sales department '); select * from emp where entrydate > (select entrydate from emp where name = ' Fang Dongbai ');Column query

select * from emp where dept_id in (select id from dept where name = ' The sales department ' or name = ' City
Field department ');select * from emp where salary > all ( select salary from emp where dept_id = (select id
from dept where name = ' Finance Department ') );select * from emp where salary > any ( select salary from emp where dept_id = (select id from dept where name = ' R & D department ') );Line sub query
select * from emp where (salary,managerid) = (select salary, managerid from emp where name = ' zhang wuji ');Table sub query
select * from emp where (job,salary) in ( select job, salary from emp where name = ' Deer stick guest ' or name = ' Song Yuanqiao ' );select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id ;Multi table query exercise
create table salgrade(
grade int,
losal int,
hisal int
) comment ' Salary scale ';
insert into salgrade values (1,0,3000);
insert into salgrade values (2,3001,5000);
insert into salgrade values (3,5001,8000);
insert into salgrade values (4,8001,10000);
insert into salgrade values (5,10001,15000);
insert into salgrade values (6,15001,20000);
insert into salgrade values (7,20001,25000);
insert into salgrade values (8,25001,30000);Implicit inner join
select e.name , e.age , e.job , d.name from emp e , dept d where e.dept_id = d.id;Explicit inner connection
select e.name , e.age , e.job , d.name from emp e inner join dept d on e.dept_id = d.id where e.age < 30;duplicate removal distinct
select distinct d.id , d.name from emp e , dept d where e.dept_id = d.id;The left outer join
select e.*, d.name from emp e left join dept d on e.dept_id = d.id where e.age > 40 ;between ... and ...
-- Mode one
select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary >=
s.losal and e.salary <= s.hisal;
-- Mode two
select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary
between s.losal and s.hisal;Three table query
select e.* , s.grade from emp e , dept d , salgrade s where e.dept_id = d.id and ( e.salary
between s.losal and s.hisal ) and d.name = ' R & D department ';function AVG
select avg(e.salary) from emp e, dept d where e.dept_id = d.id and d.name = ' R & D department ';Scalar subquery
select * from emp where salary > ( select salary from emp where name = ' extinction ' );select * from emp where salary > ( select avg(salary) from emp );select * from emp e2 where e2.salary < ( select avg(e1.salary) from emp e1 where e1.dept_id
= e2.dept_id );select d.id, d.name , ( select count(*) from emp e where e.dept_id = d.id ) ' The number of ' from dept d;Named references
select s.name , s.no , c.name from student s , student_course sc , course c where s.id =
sc.studentid and sc.courseid = c.id ;边栏推荐
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 record
- Development of NFT digital collection system: Shanxi first released digital collections of ancient buildings on "China Tourism Day"
- 5 best overseas substitutes for WPS Office
- 开发winform中遇到的一些问题汇总(持续跟新)
- NFT数字藏品系统开发:“中国旅游日”山西首次发布古建筑数字藏品
- How to become an excellent test / development programmer? Focus on planning and then move
- Tensor RT's int8 quantization principle
- 2022G1工业锅炉司炉上岗证题库及模拟考试
- Module 8 job message data MySQL table design
- 这场竞赛,能读懂题目的你大有可为
猜你喜欢

Seata 入门简介

SD NAND与eMMC优劣势对比

Arrangement of information security emergency plan

多商户商城系统功能拆解16讲-平台端会员成长值记录

Write a thesis and read this one

【MySQL从入门到精通】【高级篇】(八)聚簇索引&非聚簇索引&联合索引

Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here

Offer set (1)

The pit of mpc5744p reports an error, RTOS cannot be started, and there is a clock source problem

2022年流动式起重机司机考试试题模拟考试平台操作
随机推荐
In this competition, you who can understand the topic have great potential
MES系统的选择需重点考虑哪些方面?
JS刷题计划——数组
[yuntu said] issue 246 digital asset chain - your God of digital asset property protection!
Lombok常用注解
开发winform中遇到的一些问题汇总(持续跟新)
Lombok common notes
如何成为一名优秀的测试/开发程序员?专注谋定而后动......
MySQL学习笔记-2.如何提高sql语句的查询性能
The class jointly built by famous oarsmen is new, and Professor qiuxipeng of Fudan University broadcast it live on Tuesday!
Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here
5 best overseas substitutes for WPS Office
NFT数字藏品系统开发:上线即售罄,网民“秒杀”数字藏品
2022年化工自动化控制仪表考题模拟考试平台操作
数据安全知识体系
Flex layout
Visual VM positioning oom, fullgc usage
详细介绍@GetMapping和@PostMapping的区别
MES系统最全介绍来了
js map使用