mysql-删除、查询和分组聚合
delete
delete from [table_name] where [条件];
delete语句属于物理删除,还有逻辑删除(在表中设置一个字段来标识已删除的记录)
--清除所有表数据,表结构保留
truncate table table_name
--删除数据表,包括数据和表结构
drop table table_name
查询
-
模糊查询like,一般用来匹配字符串
- % : 匹配任意个字符
- _ : 匹配任意单个字符
-
范围查询
- in : 查询非连续范围内的数据
select * from students where hometown in ('北京 ','上海 ','广东 '); - between ... and : 查询连续范围内的数据
select * from students where age not between 20 and 25;
- in : 查询非连续范围内的数据
-
为空判断:is null
-
非空判断:is not null
-
分页查询
select * from 表名 limit start(0),count;
查询n页每页m条
select * from 表名 limit (n-1)*m, m;
分组聚合 -
聚合函数(聚合函数不能在where语句中使用)
- count()
- max()
- min()
- sum()
- avg()
-
分组查询:group by
select 字段1,字段2,聚合函数...from 表名 group by 字段1,字段2,... -
分组后的数据筛选
-
将分组后的数据当作一个表数据,再通过having条件对其进行筛选
-
having后可以使用聚合函数
select class, sex,count(*) from students group by class,sex having count(*)>1;

浙公网安备 33010602011771号