mysql(三)
- mysql(三)
- 数据准备
- 筛选条件
- 范围查询
- 模糊查询
- 排序/去重
- 聚合/分组
- 限制与分页
- 作业
数据准备
筛选条件
- 范围查询:
BETWEEN a AND b
- 间隔返回:
in
- 模糊查询:
%
匹配任意个任意的字符 - 模糊查询:
_
匹配一个任意字符
~~~mysql
范围查询
select * from student where age between 17 and 20; #找出age在这个范围的字段
select * from student where age in (17, 20); #找出age属于这里面的字段
模糊查询
select * from student where name like ‘周%’; #%匹配任意个字符
select * from student where name like ‘周_’; #匹配一个任意字符
~~~
排序/去重
- 排序:
SELECT columns FROM tb_name ORDER BY ord_col_1 [asc/desc];
- 去重:
SELECT DISTINCT columns FROM tb_name;
聚合/分组
- 聚合
常用聚合函数 | 描述 |
| 统计个数 |
| 最大值 |
| 最小值 |
| 求和 |
| 平均值 |
| 列出字段全部值 |
~mysql
select count(*) from student; #统计有几条数据
select max(age) from student; #求年龄最大值
select avg(age) from student; #求平均年龄 对于字符字段求值为0
select group_concat(age) from student; #显示字段所有值
~
- 分组
group by
在分组的情况下,只能出现聚合列和分组列
- 聚合过滤
having
对聚合出来的数据进行过滤
- where和 having的区别和组合使用
- where 不可以使用别名, having可以
- where不能操作聚合函数
- where和having组合使用是先执行where筛选数据,最后用having筛选数据
- where要写在group by 前面 having要写在group by 后面
限制与分页
作业