MySQL学习-基础篇-多表查询

目录

p37-基础-多表查询-多表关系介绍

p38-基础-多表查询-概述

p39-基础-多表查询-内连接

p40-基础-多表查询-外连接

p41-基础-多表查询-自连接

p42-基础-多表查询-联合查询union

p43-基础-多表查询-子查询介绍

p44-基础-多表查询-标量子查询

p45-基础-多表查询-列子查询

p46-基础-多表查询-行子查询

p47-基础-多表查询-表子查询

p48-基础-多表查询-练习

p50-基础-多表查询-小结


p37-基础-多表查询-多表关系介绍

 

 

create table student(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
)comment '学生表';
insert into student values (null, '黛绮丝', '2000100101'),(null, '谢逊', '2000100102'),(null, '殷天正', '2000100103'),(null, '韦一笑', '2000100104');


create table course(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '课程名称'
)comment '课程表';
insert into course values (null,'Java'),(null,'PHP'),(null,'MySQL'),(null,'Hadoop');

create table student_course(
    id int auto_increment comment '主键' primary key ,
    studentid int not null comment '学生ID',
    courseid  int not null comment '课程ID',
    constraint fk_courseid foreign key (courseid) references course(id),
    constraint fk_studentid foreign key (studentid) references student(id)
)comment '学生课程中间表';

insert into student_course values (null,1,1),(null,1,2),(null,1,3),(null,2,2),(null,2,3),(null,3,4);

 

 

 

create table tb_user(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    age int comment '年龄',
    gender char(1) comment '1: 男 , 2: 女',
    phone char(11) comment '手机号'
) comment '用户基本信息表';

create table tb_user_edu(
    id int auto_increment primary key comment '主键ID',
    degree varchar(20) comment '学历',
    major varchar(50) comment '专业',
    primaryschool varchar(50) comment '小学',
    middleschool varchar(50) comment '中学',
    university varchar(50) comment '大学',
    userid int unique comment '用户ID',
    constraint fk_userid foreign key (userid) references tb_user(id)
) comment '用户教育信息表';
insert into tb_user(id, name, age, gender, phone) values
        (null,'黄渤',45,'1','18800001111'),
        (null,'冰冰',35,'2','18800002222'),
        (null,'码云',55,'1','18800008888'),
        (null,'李彦宏',50,'1','18800009999');

insert into tb_user_edu(id, degree, major, primaryschool, middleschool, university, userid) values
        (null,'本科','舞蹈','静安区第一小学','静安区第一中学','北京舞蹈学院',1),
        (null,'硕士','表演','朝阳区第一小学','朝阳区第一中学','北京电影学院',2),
        (null,'本科','英语','杭州市第一小学','杭州市第一中学','杭州师范大学',3),
        (null,'本科','应用数学','阳泉第一小学','阳泉区第一中学','清华大学',4);

p38-基础-多表查询-概述

 

select * from dept,emp;

select * from dept,emp where emp.dept_id = dept.id;

p39-基础-多表查询-内连接

-- 内连接演示
-- 1. 查询每一个员工的姓名,及关联的部门名称(隐式内连接实现)
-- 表结构:emp , dept
-- 连接条件:emp.dept_id = dept.id
select emp.name, dept.name from emp,dept where emp.dept_id = dept.id;

-- 给表起一个别名 方便
select e.name, d.name from emp e ,dept d where e.dept_id = d.id;

-- 2. 查询每一个员工的姓名,及关联的部门的名称(显式内连接实现)
select e.name,d.name from emp e inner join dept d on e.dept_id = d.id;

select e.name,d.name from emp e  join dept d on e.dept_id = d.id; -- inner可以省略

p40-基础-多表查询-外连接

-- 外连接演示
-- 1. 查询emp表的所有数据,和对应的部门信息(左外连接)
-- 表结构:emp,dept
-- 连接条件:emp.dept_id = dept.id
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; -- outer可以省略

-- 2.查询dept表的所有数据,和对应的员工信息(右外连接)
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;

p41-基础-多表查询-自连接

-- 自连接
-- 1. 查询员工 及其 所属领导的名字
-- 表结构:emp
select a.name, b.name from emp a ,emp b where a.managerid = b.id;

-- 2. 查询所有员工emp 及其领导的名字emp,如果员工没有领导,也需要查询出来
-- 表结构: emp a , emp b
select a.name '员工', b.name '领导' from emp a left join emp b on a.managerid = b.id;

p42-基础-多表查询-联合查询union

-- 联合查询 union all , union
-- 1. 将薪资低于5000的员工, 和 年龄大于50岁的员工全部查询出来。
select * from emp where salary<5000
union all
select * from emp where age>50;

-- 去掉all 可以去重
select * from emp where salary<5000
union all
select * from emp where age>50;

p43-基础-多表查询-子查询介绍

p44-基础-多表查询-标量子查询

-- 标量子查询
-- 1. 查询‘销售部’的所有员工信息
-- a. 查询‘销售部’部门ID
select id from dept where name = '销售部';

-- b. 根据销售部部门ID,查询员工信息
select * from emp where dept_id = (select id from dept where name = '销售部');

-- 2. 查询在‘方东白’入职之后的员工信息
-- a. 查询 方东白 的入职日期
select entrydate from emp where name = '方东白';

-- b. 查询指定入职日期之后入职的员工信息
select * from emp where entrydate > (select entrydate from emp where name = '方东白');

p45-基础-多表查询-列子查询

-- 列子查询
-- 1. 查询‘销售部’ 和 ‘市场部’ 的所有员工信息
-- a. 查询‘销售部’ 和 ‘市场部’的部门ID
select id from dept where name = '销售部' or name = '市场部';

-- b. 根据部门ID, 查询员工信息
select * from emp where dept_id in (select id from dept where name = '销售部' or name = '市场部');

-- 2. 查询比财务部所有人工资都高的员工信息
-- a. 查询所有 财务部 人员工资
select id from dept where name = '财务部';

select salary from emp where dept_id = (select id from dept where name = '财务部');

-- b. 比财务部所有人工资都高的员工信息
select * from emp where salary > all ( select salary from emp where dept_id = (select id from dept where name = '财务部') );

 

-- 3. 查询比研发部其中任意一人工资高的员工信息
-- a. 查询研发部所有人工资
select salary from emp where dept_id = (select id from dept where name = '研发部');

-- b. 比研发部其中任意一人工资高的员工信息
select * from emp where salary > any ( select salary from emp where dept_id = (select id from dept where name = '研发部') );

p46-基础-多表查询-行子查询

-- 行子查询-----------------------
-- 1. 查询与 ‘张无忌’ 的薪资及直属领导相同的员工信息;
-- a. 查询 ‘张无忌’ 的薪资及直属领导
select salary, managerid from emp where name = '张无忌';

-- b. 查询与 ‘张无忌’ 的薪资及直属领导相同的员工信息; 以下三种方法都可
select * from emp where salary = 12500 and managerid = 1;
select * from emp where (salary,managerid) = (12500,1);
select * from emp where (salary,managerid) = (select salary, managerid from emp where name = '张无忌');

p47-基础-多表查询-表子查询

-- 表子查询 -----------------------
-- 1. 查询与 鹿杖客, 宋远桥 的职位和薪资相同的员工信息
-- a. 查询 鹿仗客,宋远桥 的职位和薪资
select job, salary from emp where name = '鹿杖客' or name = '宋远桥';

-- b. 查询与 鹿杖客,宋远桥 的职位和薪资相同的员工信息
select * from emp where (job,salary) in (select job, salary from emp where name = '鹿杖客' or name = '宋远桥');

-- 2. 查询入职日期是 2006-01-01 之后的员工信息, 及其部门信息
-- a. 入职日期是 2006-01-01 之后的员工信息
select * from emp where entrydate > '2006-01-01';

-- b. 查询这部分员工,对应的部门信息;
select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id;

p48-基础-多表查询-练习

-- 1. 查询员工的姓名、年龄、职位、部门信息 (隐式内连接)
select e.name,e.age,e.job,d.name from emp e, dept d where e.dept_id = d.id;

-- 2. 查询年龄小于30岁的员工的姓名、年龄、职位、部门信息(显式内连接)
select e.name,e.age,e.job,d.name from emp e join dept d on d.id = e.dept_id where e.age<30;

-- 3. 查询拥有员工的部门ID、部门名称
select distinct d.id, d.name from emp e, dept d where e.dept_id = d.id;

-- 4. 查询所有年龄大于40岁的员工, 及其归属的部门名称; 如果员工没有分配部门, 也需要展示出来
select e.*, d.name from emp e left join dept d on d.id = e.dept_id where e.age>40;

-- 5. 查询所有员工的工资等级
-- 连接条件: emp.salary >= salgrade.losal and emp.salary <= salgrade.hisal
select e.*, s.grade, s.losal, s.hisal from emp e, salgrade s where e.salary >= s.losal and e.salary <= s.hisal;

select e.*, s.grade, s.losal, s.hisal from emp e, salgrade s where e.salary between s.losal and s.hisal;

-- 6. 查询 "研发部" 所有员工的信息及 工资等级
-- 表: emp , salgrade , dept
-- 连接条件 : emp.salary between salgrade.losal and salgrade.hisal , emp.dept_id = dept.id
-- 查询条件 : dept.name = '研发部'
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 = '研发部';

-- 7. 查询 "研发部" 员工的平均工资
select avg(e.salary) from emp e, dept d where e.dept_id = d.id and d.name = '研发部';

-- 8. 查询工资比 "灭绝" 高的员工信息。
-- a. 查询 "灭绝" 的薪资
select salary from emp where name = '灭绝';

-- b. 查询比她工资高的员工数据
select * from emp where salary>( select salary from emp where name = '灭绝' );

-- 9. 查询比平均薪资高的员工信息
-- a. 查询员工的平均薪资
select avg(salary) from emp ;
-- b. 查询比平均薪资高的员工信息
select * from emp where salary>( select avg(salary) from emp );

-- 10. 查询低于本部门平均工资的员工信息
-- a. 查询指定部门平均薪资  1
select avg(e1.salary) from emp e1 where e1.dept_id = 1;
select avg(e1.salary) from emp e1 where e1.dept_id = 2;

-- b. 查询低于本部门平均工资的员工信息
select  * from emp e2 where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id);
select  *,(select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id) '平均' from emp e2 where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id);

-- 11. 查询所有的部门信息, 并统计部门的员工人数
select d.id,d.name, (select count(*) from emp e where e.dept_id = d.id) '人数' from dept d;

select count(*) from emp where dept_id = 1;

-- 12. 查询所有学生的选课情况, 展示出学生名称, 学号, 课程名称
-- 表: student , course , student_course
-- 连接条件: student.id = student_course.studentid , course.id = student_course.courseid
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;

p50-基础-多表查询-小结

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值