mysql常用命令(3)简单查询

查询指定的行数据:

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;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、SQL速成 结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件...
    shadow雨轩阅读 523评论 0 3
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,256评论 0 7
  • 笔记: 一、聚合函数:计数 最大值 最小值 平均数 求和 1.计数 COUNT() 忽略NULL值 方式1:COU...
    凤之鸠阅读 5,295评论 0 1
  • 南昌今天14度啊,要添衣服了 简简单单的一句话,让小宝贝开心了一天,其实不知道从什么时候开始,手机里的天气就变成了...
    傻大个的小胖子阅读 138评论 0 0
  • 我们是姐弟两,斧头是姐姐,聚聚嘛,当然是弟弟喽,姐姐比弟弟大5个月。我猫咪名字很特别吧?宠物医院的美眉说,真容易记...
    桂之华阅读 166评论 4 2