寫在前面: 以下是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 by前 where
分組後篩選 group by後的結果集 group by後 having
# 每個工種有獎金的員工的最高工資>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 查詢列表
from 表1 別名 【連接類型】
join 表2 別名
on 連接條件
【where 篩選條件】
【group by 分組】
【having 篩選條件】
【order by 排序列表】
#內連接(★):inner
#外連接
左外(★):left 【outer】
右外(★):right 【outer】
全外:full【outer】
#交叉連接: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 join 表2
on 連接條件
where 篩選條件
group by 分組字段
having 分組後的篩選
order by 排序的字段】
limit 【offset,】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 查詢列表 ⑦
from 表1 別名 ①
連接類型 join 表2 ②
on 連接條件 ③
where 篩選 ④
group by 分組列表 ⑤
having 篩選 ⑥
order by排序列表 ⑧
limit 起始條目索引,條目數; ⑨