MySql—查询四
- 联合查询
- DML语言(数据操作语言—增删改查)
- DDL (数据定义–库和表的管理)
- 常见数据类型
温故知新
######每日复习
#四、SQL语法:
1.内连接
语法:
select 查询列表
from 表1 别名
【inner】 join 表2 别名 on 连接条件
where 筛选条件
group by 分组列表
having 分组后的筛选
order by 排序列表
limit 偏移,显示的数据的条数;
特点:
》表的顺序可以交换
》内连接的结果=多表的交集
》n表连接至少需要n-1个连接条件
分类:
等值连接
非等值连接
自连接
2.外连接
select 查询列表
from 表1 别名
left|right|full【outer 】 join 表2 别名 on 连接条件
where 筛选条件
group by 分组列表
having 分组后的筛选
order by 排序列表
limit 偏移,显示的数据的条数;
特点:
》查询的结果= 主表中所有的行,如果从表和它匹配的将显示是匹配行,如果从表没有匹配的则显示null
》left join 左边的就是主表,right join右边的就是主表
full join两边都是主表
》一般用于查询除了交集部分的剩余的不匹配的行
3.交叉连接
语法:
select 查询列表
from 表1 别名
cross join 表2 别名;
特点:
类似于笛卡尔乘积
######################
子查询
一、含义:
嵌套在其他语句内部的select 语句称为子查询或内查询,外面的语句可以是 insert update delete select 等,一般select作为外面语句较多
外面语句如果为select语句,则此语句称为外查询或主查询
二、分类
1.按出现位置
select后面:仅仅支持标量子查询
from后面:表子查询
where或having后面:标量子查询、列子查询、行子查询
exists后面:标量子查询,列子查询,行子查询,表子查询
2.按结果集的行列
标量子查询(单行子查询):结果集为一行一列
列子查询(多行子查询):结果集为多行一列
行子查询:结果集为多行多列
表子查询:结果集为多行多列
三、示例
where 或having 后面
1.标量子查询
案例:查询最低工资 的员工姓名和工资
1)最低工资
select min(salary) from employees
2)查询员工的姓名和工资,要求工资=1)
select last_name,salary
from employees
where salary=(
select min(salary) from employees
);
2.列子查询
案例:查询所有是领导的员工姓名
1)查询所有员工的manager_id
select manager_id
from employees
2)查询姓名,employee_id属于1)列表的一个
select last_name
from employees
where employee_id in(
select manager_id
from employees
);
#######################
分页查询
一、应用场景:
当查询的条目数太多,一页显示不全
二、语法:
select 查询列表
from 表
limit [offset,] size;
注意:
offset代表的是起始的条目索引,默认从0卡死
size代表的是显示的条目数
公式:
加入要显示的页数为page,每一页条目数为size
select 查询列表
from 表
limit [page-1]*size,size;
大练兵1
###经典例题讲解
#1.查询工资最低的员工信息:last_name,salary
#1)查询最低的工资
select min(salary)
from employees
#2)查询last_name,salary,要求salary=1)
select last_name,salary
from employees
where salary=(
select min(salary)
from employees
);
#2.查询平均工资最低的部门信息
#方式一:
#1)各部门的平均工资
select avg(salary),department_id
from employees
group by department_id
#2)查询1)结果上的最低平均工资
select min(ag)
from (
select avg(salary) as ag,department_id
from employees
group by department_id
)as ag_dep
##3)查询哪个部门的平均工资=2)
select avg(salary),department_id
from employees
group by department_id
having avg(salary) = (
select min(ag)
from (
select avg(salary) as ag,department_id
from employees
group by department_id
)as ag_dep
);
#查询部门信息
select d.*
from departments as d
where d.`department_id`=(
select avg(salary),department_id
from employees
group by department_id
having avg(salary) = (
select min(ag)
from (
select avg(salary) as ag,department_id
from employees
group by department_id
)as ag_dep
)
);
#方式二:
#1)各个部门的平均工资
select avg(salary),department_id
from employees
group by departmnent_id
#2)求出最低平均工资的部门编号
select department_id
from eployees
group by department_id
order by avg(salary)
limit 1;
#3)查询部门信息
select *
from departments
where department_id = (
select department_id
from eployees
group by department_id
order by avg(salary)
limit 1;
);
#3.查询平均工资最低的部门信息和该部门的平均工资
#1)各个部门的平均工资
select avg(salary),department_id
from employees
group by departmnent_id
#2)求出最低平均工资的部门编号
select avg(salary),department_id
from eployees
group by department_id
order by avg(salary)
limit 1;
#3)查询部门信息
select d.*,ag
from departments as d
join (
select avg(salary) as ag,department_id
from eployees
group by department_id
order by avg(salary)
limit 1;
)as ag_dep
on d.`department_id` = ag_dep.department_id;
#4.查询平均工资最高的job信息
#1)查询最高的job的平均工资
select avg(salary),job_id
from employees
group by job_id
order by avg(salary) desc
limit 1
#2)查询job信息
select *
from jobs
where job_id =(
select job_id
from employees
group by job_id
order by avg(salary) desc
limit 1
);
#5.查询平均工资高于公司平均工资的部门有哪些?
#1)查询平均工资
select avg(salary)
from employees
#2)查看每个部门的平均工资
select avg(salary),department_id
from employees
group by department_id
#3)筛选2)的结果集,满足平均工资>1)
select avg(salary),department_id
from employees
group by department_id
having avg(salary)>(
select avg(salary)
from employees
);
#6.查询出公司中所有manager的详细信息
#1)查询所有manager的员工编号
select distinct manager_id
from employees
#2)查询详细信息,满足employee_id=1)
select *
from employee_id = any(
select distinct manager_id
from employees
);
#7.各个部门中最高工资中最低的哪个部门的最低最低工资是多少
#1)查询各部门的最高工资中最低的部门编号
select department_id
from empoyees
group by department_id
order by max