码迷,mamicode.com
首页 > 数据库 > 详细

mysql_group by与聚合函数、order by联合使用

时间:2019-04-04 20:41:00      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:dial   mys   1.2   _id   cal   ase   height   desc   排列   

1、group by:后接字段名,根据字段对数据进行分组

SQL语句:select task_id,session_id,customer_case_id,callout_connect_status from callout_session where callout_dial_time between ‘2019-04-01 00:00:000‘ and ‘2019-04-03 23:59:59‘ group by task_id,session_id

    技术图片

        表1

1.1、单独使用group by 列名,不与聚合函数联合使用

  group by后面跟一个列名task_id,起到了去重的作用,将task_id值相同的行合并成一行显示

    技术图片

      表1.1-1

  group by后面跟两个列名task_id、customer_id:同样是去重作用,将同时满足task_id值相同、customer_id值相同的行合并成一行

    注:这里不是合并task_id值与customer_id值相同的行,博主本人以前在这里就理解错了

    技术图片

      表1.1-2

1.2、group by与聚合函数使用

sql语句:select task_id,count(task_id),session_id,customer_case_id,callout_connect_status from callout_session where callout_dial_time between ‘2019-04-01 9-04-01 00:00:00‘ and ‘2019-04-03 23:59:59‘ group by task_id order by task_id,customer_case_id;

  count(task_id) 这里统计了task_id值相同的行数量,与表1数据进行对比

    技术图片

 2、order by:对查询结果进行排序,后面跟字段名

    order by 字段名 desc:降序排列

    order by 字段名 asc:升序排列

 

mysql_group by与聚合函数、order by联合使用

标签:dial   mys   1.2   _id   cal   ase   height   desc   排列   

原文地址:https://www.cnblogs.com/jingsx/p/10656748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!