DQL关键字 | 含义 |
---|---|
SELECT | SELECT 字段列表 ,查询检索的列或表达式,它指定了最终结果包含的列 |
FROM | FROM 表名 ,指定了要查询的列表或视图,例FROM table_name |
WHERE | WHERE 条件列表 ,筛选查询结果,通常是列(字段)的条件表达式,例:WHERE id = 10 |
GROUP BY | GROUP BY 分组字段列表 ,根据一个或多个列对结果进行分组,但是通常需要满足聚合条件 |
HAVING | HAVING 分组后的条件列表 ,用于在GROUP BY子句的结果上进行过滤筛选 |
ORDER BY | ORDER BY 排序字段列表 ,用于按照一个或多个列对结果进行排序,它可以按升序(ASC)或降序(DESC)对查询结果进行排序 |
LIMIT | LIMIT 分页参数 ,一般在语句的最后,用于限制查询结果的数量 |
DQL执行顺序 | FROM -> WHERE -> GROUP BY -> HAVING ->SELECT -> ORDER BY -> LIMIT |
查表之前首先要选择库use database_name
查看emp表全部列
select * from emp5;
查看name字段
select name from emp5
+----+--------+---------------------+------+-------+
| id | name | time | age | wages |
+----+--------+---------------------+------+-------+
| 1 | 张三 | 2024-01-25 15:24:19 | 18 | 2000 |
| 2 | 倪哥 | 2024-01-25 15:34:11 | 20 | 1000 |
| 3 | 李四 | 2024-01-25 16:14:19 | 18 | 2000 |
| 4 | 陈五 | 2024-01-25 16:14:19 | 19 | 1000 |
| 5 | 王六 | 2024-01-25 16:14:19 | 19 | 2000 |
| 6 | 横七 | 2024-01-25 16:14:19 | 20 | 1000 |
| 7 | 竖八 | 2024-01-25 16:14:19 | 20 | 2200 |
+----+--------+---------------------+------+-------+
-- 查询最大工资
select max(wages) from emp5;
+------------+
| max(wages) |
+------------+
| 2200 |
+------------+
-- 将最大工资作为条件插入查询name字段的语句中
select name from emp5 where wages = (select max(wages) from emp5);
+--------+
| name |
+--------+
| 竖八 |
+--------+
-- 多个字段同理
select name,age,wages from emp5 where wages = (select max(wages) from emp5);
+--------+------+-------+
| name | age | wages |
+--------+------+-------+
| 竖八 | 20 | 2200 |
+--------+------+-------+
-- 查询部门表
select * from dep
+-----+--------------+
| id | name |
+-----+--------------+
| 200 | 技术部 |
| 201 | 人力资源 |
| 202 | 销售部 |
| 203 | 运营部 |
| 204 | 售后部 |
+-----+--------------+
-- 查询员工表
select * from emp5;
+----+--------+---------------------+------+-------+--------+
| id | name | time | age | wages | dep_id |
+----+--------+---------------------+------+-------+--------+
| 1 | 张三 | 2024-01-25 15:24:19 | 18 | 2000 | 203 |
| 2 | 倪哥 | 2024-01-25 15:34:11 | 20 | 1000 | 201 |
| 3 | 李四 | 2024-01-25 16:14:19 | 18 | 2000 | 202 |
| 4 | 陈五 | 2024-01-25 16:14:19 | 19 | 1000 | 203 |
| 5 | 王六 | 2024-01-25 16:14:19 | 19 | 2000 | 201 |
| 6 | 横七 | 2024-01-25 16:14:19 | 20 | 1000 | 200 |
| 7 | 竖八 | 2024-01-25 16:14:19 | 20 | 2200 | 205 |
+----+--------+---------------------+------+-------+--------+
-- 联表查询dep和emp5
select * from dep,emp5;
+-----+--------------+----+--------+---------------------+------+-------+--------+
| id | name | id | name | time | age | wages | dep_id |
+-----+--------------+----+--------+---------------------+------+-------+--------+
| 204 | 售后部 | 1 | 张三 | 2024-01-25 15:24:19 | 18 | 2000 | 203 |
| 203 | 运营部 | 1 | 张三 | 2024-01-25 15:24:19 | 18 | 2000 | 203 |
| 202 | 销售部 | 1 | 张三 | 2024-01-25 15:24:19 | 18 | 2000 | 203 |
| 201 | 人力资源 | 1 | 张三 | 2024-01-25 15:24:19 | 18 | 2000 | 203 |
| 200 | 技术部 | 1 | 张三 | 2024-01-25 15:24:19 | 18 | 2000 | 203 |
| 204 | 售后部 | 2 | 倪哥 | 2024-01-25 15:34:11 | 20 | 1000 | 201 |
| 203 | 运营部 | 2 | 倪哥 | 2024-01-25 15:34:11 | 20 | 1000 | 201 |
| 202 | 销售部 | 2 | 倪哥 | 2024-01-25 15:34:11 | 20 | 1000 | 201 |
| 201 | 人力资源 | 2 | 倪哥 | 2024-01-25 15:34:11 | 20 | 1000 | 201 |
| 200 | 技术部 | 2 | 倪哥 | 2024-01-25 15:34:11 | 20 | 1000 | 201 |
| 204 | 售后部 | 3 | 李四 | 2024-01-25 16:14:19 | 18 | 2000 | 202 |
| 203 | 运营部 | 3 | 李四 | 2024-01-25 16:14:19 | 18 | 2000 | 202 |
| 202 | 销售部 | 3 | 李四 | 2024-01-25 16:14:19 | 18 | 2000 | 202 |
| 201 | 人力资源 | 3 | 李四 | 2024-01-25 16:14:19 | 18 | 2000 | 202 |
| 200 | 技术部 | 3 | 李四 | 2024-01-25 16:14:19 | 18 | 2000 | 202 |
| 204 | 售后部 | 4 | 陈五 | 2024-01-25 16:14:19 | 19 | 1000 | 203 |
| 203 | 运营部 | 4 | 陈五 | 2024-01-25 16:14:19 | 19 | 1000 | 203 |
| 202 | 销售部 | 4 | 陈五 | 2024-01-25 16:14:19 | 19 | 1000 | 203 |
| 201 | 人力资源 | 4 | 陈五 | 2024-01-25 16:14:19 | 19 | 1000 | 203 |
| 200 | 技术部 | 4 | 陈五 | 2024-01-25 16:14:19 | 19 | 1000 | 203 |
| 204 | 售后部 | 5 | 王六 | 2024-01-25 16:14:19 | 19 | 2000 | 201 |
| 203 | 运营部 | 5 | 王六 | 2024-01-25 16:14:19 | 19 | 2000 | 201 |
| 202 | 销售部 | 5 | 王六 | 2024-01-25 16:14:19 | 19 | 2000 | 201 |
| 201 | 人力资源 | 5 | 王六 | 2024-01-25 16:14:19 | 19 | 2000 | 201 |
| 200 | 技术部 | 5 | 王六 | 2024-01-25 16:14:19 | 19 | 2000 | 201 |
| 204 | 售后部 | 6 | 横七 | 2024-01-25 16:14:19 | 20 | 1000 | 200 |
| 203 | 运营部 | 6 | 横七 | 2024-01-25 16:14:19 | 20 | 1000 | 200 |
| 202 | 销售部 | 6 | 横七 | 2024-01-25 16:14:19 | 20 | 1000 | 200 |
| 201 | 人力资源 | 6 | 横七 | 2024-01-25 16:14:19 | 20 | 1000 | 200 |
| 200 | 技术部 | 6 | 横七 | 2024-01-25 16:14:19 | 20 | 1000 | 200 |
| 204 | 售后部 | 7 | 竖八 | 2024-01-25 16:14:19 | 20 | 2200 | 205 |
| 203 | 运营部 | 7 | 竖八 | 2024-01-25 16:14:19 | 20 | 2200 | 205 |
| 202 | 销售部 | 7 | 竖八 | 2024-01-25 16:14:19 | 20 | 2200 | 205 |
| 201 | 人力资源 | 7 | 竖八 | 2024-01-25 16:14:19 | 20 | 2200 | 205 |
| 200 | 技术部 | 7 | 竖八 | 2024-01-25 16:14:19 | 20 | 2200 | 205 |
+-----+--------------+----+--------+---------------------+------+-------+--------+
-- 添加条件限制,当员工表中的部门id = 部门表中id时,如果不存在对应的部门则不打印(比如id7的205部门)
select * from dep,emp5 where emp5.dep_id=dep.id;
+-----+--------------+----+--------+---------------------+------+-------+--------+
| id | name | id | name | time | age | wages | dep_id |
+-----+--------------+----+--------+---------------------+------+-------+--------+
| 203 | 运营部 | 1 | 张三 | 2024-01-25 15:24:19 | 18 | 2000 | 203 |
| 201 | 人力资源 | 2 | 倪哥 | 2024-01-25 15:34:11 | 20 | 1000 | 201 |
| 202 | 销售部 | 3 | 李四 | 2024-01-25 16:14:19 | 18 | 2000 | 202 |
| 203 | 运营部 | 4 | 陈五 | 2024-01-25 16:14:19 | 19 | 1000 | 203 |
| 201 | 人力资源 | 5 | 王六 | 2024-01-25 16:14:19 | 19 | 2000 | 201 |
| 200 | 技术部 | 6 | 横七 | 2024-01-25 16:14:19 | 20 | 1000 | 200 |
+-----+--------------+----+--------+---------------------+------+-------+--------+
mysql> select * from emp5 where wages in (select max(wages) from emp5 group by dep_id);
group by
作用于where
之后,order by
之前将表格emp5
以wages
(工资)分组(2000,1000,2200)
select wages from emp5 group by wages;
select dep_id,max(wages) from emp5 group by dep_id;
sum
、max
、avg
、count
等select dep_id,sum(wages) from emp5 group by dep_id;
select dep_id,max(wages) from emp5 group by dep_id;
select dep_id,avg(wages) from emp5 group by dep_id;
select dep_id,count(*) from emp5 group by dep_id;
group_concat
可以查看到组内所有人的属性
select group_concat(name), dep_id from emp5 group by dep_id;
group_concat
也可以查看进行字符的拼接
select group_concat(name,":",wages), dep_id from emp5 group by dep_id;
HAVING
一般紧随GROUP BY
之后,一般用于限定SELECT
筛选的字段select dep_id,max(wages) from emp5 group by dep_id having max(wages)>2000;
select dep_id,max(wages) from emp5 group by dep_id having dep_id in (202,203);
limit 0,2
从第0页开始,往后每页2条数据limit 2,2
每页两条数据从第二页开始查询select * from emp5 limit 0,2
ASC
DESC
select * from emp5 order by wages ASC;
select * from emp5 order by dep_id ASC, wages ASC;