查询指定的行数据:
select * from my_test limit n,m; //从第 n+1 行到第 m 行
排序查询:
根据id降序查询:
select * from videos order by id desc;//升序查询后面用 asc
order by 和 limit 一起使用
降序查询前五条:
select * from videos order by id desc limit 5;
条件查询:
select * from videos where id>=2 and id<=6;
组查询:
查询 id为 2,3,4的:
select * from videos where id in (2,3,4);
查询 id为 1 的 以及 vtype为 搞笑的:
select * from videos where id=1 or vtype="搞笑";
模糊查询:
查询创建时间里包含5的数据:
select * from videos where create_time like '%5%';
查询创建时间第二位为0的数据:
select * from videos where create_time like '_0%';
查询创建时间第二位不为0的数据:
select * from videos where create_time not like '_0%';
查询创建时间不为空的数据:
select * from videos where create_time is not null;
根据一个表中的相同vtype的条数排序查询:
select id from videos group by vtype order by count(*) desc;
根据create_time1从大到小排序:
select * from videos order by create_time desc;
求字段内容的平均值:
select avg(字段名) from videos;
根据vtype进行分组
select * from group by vtype;
类型分组 查询每一组最大的id:
select max(id) from videos group by vtype;
相同vtype分组 相同vtype数量大于2的 降序排列:
select *,count(*) from videos group by vtype having count(*)>2 order by count(*) desc; //having 放在 group by之后 where放在前
查询数据库里一共几条数据不包括重复的vtype字段的:
select count(distinct vtype) from videos;
查询vtype都有什么类型:
select distinct vtype from videos;