查詢指定的行數(shù)據(jù):
select * from my_test limit n,m; //從第 n+1 行到第 m 行
排序查詢:
根據(jù)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="搞笑";
模糊查詢:
查詢創(chuàng)建時(shí)間里包含5的數(shù)據(jù):
select * from videos where create_time like '%5%';
查詢創(chuàng)建時(shí)間第二位為0的數(shù)據(jù):
select * from videos where create_time like '_0%';
查詢創(chuàng)建時(shí)間第二位不為0的數(shù)據(jù):
select * from videos where create_time not like '_0%';
查詢創(chuàng)建時(shí)間不為空的數(shù)據(jù):
select * from videos where create_time is not null;
根據(jù)一個(gè)表中的相同vtype的條數(shù)排序查詢:
select id from videos group by vtype order by count(*) desc;
根據(jù)create_time1從大到小排序:
select * from videos order by create_time desc;
求字段內(nèi)容的平均值:
select avg(字段名) from videos;
根據(jù)vtype進(jìn)行分組
select * from group by vtype;
類型分組 查詢每一組最大的id:
select max(id) from videos group by vtype;
相同vtype分組 相同vtype數(shù)量大于2的 降序排列:
select *,count(*) from videos group by vtype having count(*)>2 order by count(*) desc; //having 放在 group by之后 where放在前
查詢數(shù)據(jù)庫里一共幾條數(shù)據(jù)不包括重復(fù)的vtype字段的:
select count(distinct vtype) from videos;
查詢vtype都有什么類型:
select distinct vtype from videos;