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

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:升序排列

猜你喜欢

转载自www.cnblogs.com/jingsx/p/10656748.html