MySQL的查询语句以及案例演示

写在前面: 以下是mysql的查询语句复习总结
公众号: 小白编码

环境:

employees表:
在这里插入图片描述
departments表:
在这里插入图片描述

查询所有:

select * from 表名;

查询指定字段:

select 字段 from 表名;

去重查询:

select distinct * from 表名;

别名查询:

select name as '书名' from t_book;
select name '书名' from t_book;

条件查询:

select 查询列表 from 表面 where 筛选条件;
#搭配条件表达式:> < = != <> >= <= 	&& || ! and or not
#如:查询id值为1的书名
select name from book where id = 1;

模糊查询:

#查询书名中包含字符a的书籍信息
select * from book where name like '%a%';
#查询员工编号在100到120之间的员工信息
select * from employees where employee_id between 120 and 100;
#查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
select * from employees where job_id in( 'IT_PROT' ,'AD_VP','AD_PRES');
#is null 和 is not null
#查询有奖金的员工名
select * from employees where commission_pct is not null;

排序查询:

# 语法
select 查询列表
from 表名
【where  筛选条件】
order by 排序的字段或表达式; 
#其中:
asc代表的是升序,可以省略
desc代表的是降序
#案例:查询员工信息,要求先按工资降序,再按employee_id升序
select * 
from employees 
order by salary desc,emp_id asc;

分组函数:

#sum 求和、avg 平均值、max 最大值 、min 最小值 、count 计算个数
select sum(salary) from emp;
select avg(salary) from emp;
select min(salary) from emp;
select max(salary) from emp;
select count(*) from emp;

分组查询:

select 查询列表
from 表
【where 筛选条件】
group by 分组的字段
【order by 排序的字段】;

特点:
1、和分组函数一同查询的字段必须是group by后出现的字段
2、筛选分为两类:分组前筛选和分组后筛选
		针对的表			位置		连接的关键字
分组前筛选	原始表				group bywhere
	
分组后筛选	group by后的结果集    		group byhaving

# 每个工种有奖金的员工的最高工资>6000的工种编号和最高工资,按最高工资升序
SELECT MAX(salary),job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>6000
ORDER BY MAX(salary);

连接查询:(sql99语法)

select 查询列表
	from1 别名 【连接类型】
	join2 别名 
	on 连接条件
	【where 筛选条件】
	【group by 分组】
	【having 筛选条件】
	【order by 排序列表】
#内连接(★):inner
#外连接
	左外():leftouter】
	右外()rightouter】
	全外:fullouter#交叉连接:cross 

#.查询哪个部门的员工个数>3的部门名和员工个数,并按个数降序(添加排序)
select count(*),dept_name 
from emp e
inner join dept d
on e.dept_id = d.dept_id
group by dept_name
having count(*) > 3
order by count(*) desc;

#非等值连接练习:查询工资级别的个数>20的个数,并且按工资级别降序
select count(*),grade_level
from emp e
join job_grades g
on e.salary between g.lowest_sal and g.highest_sal
group by grade_level
having count(*) > 20
order by grade_level desc;

#自连接:查询姓名中包含字符k的员工的名字、上级的名字
select e.last_name,m.last_name
from employees e
join employees m
on e.manager_id = m.manager_id
where e.last_name like '%k%';

#查询哪个部门没有员工
#左外
select d.*,employee_id
from departments d 
left outer join employees e 
on d.department_id =e.department_id
where e.employee_id is null;
#右外
 SELECT d.*,e.employee_id
 FROM employees e
 RIGHT OUTER JOIN departments d
 ON d.`department_id` = e.`department_id`
 WHERE e.`employee_id` IS NULL;

分页查询:

select 查询列表
from 表
【join type join2
on 连接条件
where 筛选条件
group by 分组字段
having 分组后的筛选
order by 排序的字段】
limitoffset,】size;

offset要显示条目的起始索引(起始索引从0开始)
size 要显示的条目个数
#查询第11条——第25条
select * from employees limit 10,15;
#有奖金的员工信息,并且工资较高的前10名显示出来
select * from employees 
where commission_pct is not null
order by salary desc
limit 10;

联合查询:

语法:
查询语句1
union
查询语句2
union
...

#查询部门编号>90或邮箱包含a的员工信息
select * from employees where email like '%a%' or department_id >90;
#使用联合:
SELECT * FROM employees  WHERE email LIKE '%a%'
UNION
SELECT * FROM employees  WHERE department_id>90;

子查询:

分类:
按子查询出现的位置:
	select后面:
		仅仅支持标量子查询
	

from后面:
	支持表子查询
where或having后面:★
	标量子查询(单行) √
	列子查询  (多行) √
	
	行子查询
	
exists后面(相关子查询)
	表子查询

按结果集的行列数不同:
	标量子查询(结果集只有一行一列)
	列子查询(结果集只有一列多行)
	行子查询(结果集有一行多列)
	表子查询(结果集一般为多行多列)
#查询最低工资大于50号部门最低工资的部门id和其最低工资
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
	SELECT  MIN(salary)
	FROM employees
	WHERE department_id = 50
);

#查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
SELECT employee_id,last_name,salary,e.department_id
FROM employees e
INNER JOIN (
	SELECT AVG(salary) ag,department_id
	FROM employees
	GROUP BY department_id


) ag_dep
ON e.department_id = ag_dep.department_id
WHERE salary>ag_dep.ag ;

#查询在部门的location_id为1700的部门工作的员工的员工号
SELECT employee_id
FROM employees
WHERE department_id =ANY(
	SELECT DISTINCT department_id
	FROM departments 
	WHERE location_id  = 1700

);
#查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
SELECT last_name,employee_id
FROM employees
WHERE department_id IN(
	SELECT  DISTINCT department_id
	FROM employees
	WHERE last_name LIKE '%u%'
);

#查询每个部门的平均工资的工资等级

SELECT  ag_dep.*,g.`grade_level`
FROM (
	SELECT AVG(salary) ag,department_id
	FROM employees
	GROUP BY department_id
) ag_dep
INNER JOIN job_grades g
ON ag_dep.ag BETWEEN lowest_sal AND highest_sal;

查询流程:

语法:
select 查询列表    ⑦		
from1 别名       ①
连接类型 join2   ②
on 连接条件         ③
where 筛选          ④
group by 分组列表   ⑤
having 筛选         ⑥
order by排序列表    ⑧
limit 起始条目索引,条目数;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章