场景:
给予一张商品售卖表,表中数据为商品的售卖记录,假设表中数据是定时脚本插入的,每个时间段的商品售卖数量不同,根据此表找各个商品的最多售卖数量的数据。

1、数据表

CREATE TABLE `goods_sell` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`goods_id` int(10) unsigned NOT NULL DEFAULT '0',
`sell_num` int(10) unsigned NOT NULL DEFAULT '0',
`create_time` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

2、数据内容

mysql> select * from goods_sell;
+----+----------+----------+-------------+
| id | goods_id | sell_num | create_time |
+----+----------+----------+-------------+
| 1 | 1 | 5 | 1420520010 |
| 2 | 2 | 10 | 1420520000 |
| 3 | 1 | 10 | 1410520000 |
| 4 | 1 | 5 | 1510520000 |
| 5 | 2 | 6 | 1510521000 |
| 6 | 3 | 15 | 1510621000 |
+----+----------+----------+-------------+
6 rows in set (0.00 sec)

剖析其要求,也就是说,要用1条sql
找出goods_id 为1 的id为4的数据
找出goods_id 为2 的id为2的数据
找出goods_id 为3 的id为6的数据

3、怎么做呢?
这时就可以用MySQL的分组聚合,GROUP_CONCAT和SUBSTRING_INDEX一起使用。
#查找各个商品售卖最多的一条记录,此时group_concat()中一定要order by排序。要不然截取第一个数据就不对了。

select id,goods_id,
SUBSTRING_INDEX(GROUP_CONCAT(sell_num order by sell_num desc),',',1) sell_num_max,
create_time
from goods_sell group by goods_id order by create_time DESC; +----+----------+--------------+-------------+
| id | goods_id | sell_num_max | create_time |
+----+----------+--------------+-------------+
| 6 | 3 | 15 | 1510621000 |
| 1 | 1 | 10 | 1420520010 |
| 2 | 2 | 10 | 1420520000 |
+----+----------+--------------+-------------+
3 rows in set (0.00 sec)

不使用SUBSTRING_INDEX的话,查出来的数据是:

select id,goods_id, GROUP_CONCAT(sell_num order by sell_num desc) sell_num_list, create_time  from good;

+----+----------+---------------+-------------+
| id | goods_id | sell_num_list | create_time |
+----+----------+---------------+-------------+
| 6 | 3 | 15 | 1510621000 |
| 1 | 1 | 10,5,5 | 1420520010 |
| 2 | 2 | 10,6 | 1420520000 |
+----+----------+---------------+-------------+
3 rows in set (0.00 sec)

所以用SUBSTRING_INDEX截取最前面的一个数据。

文档参考:
https://www.cnblogs.com/zhwbqd/p/4205821.html
https://blog.csdn.net/m0_37797991/article/details/80511855
https://baijiahao.baidu.com/s?id=1595349117525189591&wfr=spider&for=pc

MySQL分组聚合group_concat + substr_index的更多相关文章

  1. Mysql 分组聚合实现 over partition by 功能

    mysql中没有类似oracle和postgreSQL的 OVER(PARTITION BY)功能. 那么如何在MYSQL中搞定分组聚合的查询呢 先说结论: 利用 group_concat + sub ...

  2. mysql分组合并GROUP_CONCAT

    SELECT  pid,  GROUP_CONCAT(field ORDER BY  coder desc)  'msg'from   product_field    GROUP BY  pid 分 ...

  3. MySQL最常用分组聚合函数

    一.聚合函数(aggregation function)---也就是组函数 在一个行的集合(一组行)上进行操作,对每个组给一个结果. 常用的组函数: AVG([distinct] expr) 求平均值 ...

  4. mysql 分组和聚合函数

    mysql 分组和聚合函数 Mysql 聚集函数有5个: 1.COUNT() 记录个数(count(1),count(*)统计表中行数,count(列名)统计列中非null数) 2.MAX() 最大值 ...

  5. mysql数据库优化课程---10、mysql数据库分组聚合

    mysql数据库优化课程---10.mysql数据库分组聚合 一.总结 一句话总结:select concat(class,' 班') 班级,concat(count(*),' 人') 人数 from ...

  6. 浅析MySQL使用 GROUP BY 分组聚合与细分聚合

    原创文章,转载请注明出处:http://www.cnblogs.com/weix-l/p/7521278.html: 若有错误,请评论指出,谢谢! 1. 聚合函数(Aggregate Function ...

  7. Oracle和MySQL分组查询GROUP BY

    Oracle和MySQL分组查询GROUP BY 真题1.Oracle和MySQL中的分组(GROUP BY)有什么区别? 答案:Oracle对于GROUP BY是严格的,所有要SELECT出来的字段 ...

  8. MySQL之聚合数据(AVG,COUNT,MAX,MIN,SUM)

    1.首先我们需要了解下什么是聚合函数 聚合函数aggregation function又称为组函数. 认情况下 聚合函数会对当前所在表当做一个组进行统计. 2.聚合函数的特点 1.每个组函数接收一个参 ...

  9. mysql concat和group_concat

    mysql concat(str1,str2...)连接两个字符串,(数字也是可以的,会转成字符串) MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL mys ...

随机推荐

  1. Policy Improvement and Policy Iteration

    From the last post, we know how to evaluate a policy. But that's not enough, because the purpose of ...

  2. 前端 CSS 一些标签默认有padding

    一个html body标签 默认有 margin外边距属性 比如ul标签,有默认的padding-left值. 那么我们一般在做站的时候,是要清除页面标签中默认的padding和margin.以便于我 ...

  3. Java之九九乘法表

    public class MultiplicationTable { public static void main(String[] args) { for(int i=1;i<=9;i++) ...

  4. Jquery中$(function(){})

    1. 在哪书写js文件 如果我们要执行一段js代码,我们该怎么办呢? 1.我们可以写一个js文件,在js文件里写执行函数,然后再<script src='...'> ... </sc ...

  5. Bloxorz I (poj3322) (BFS)

    [题目描述] It's a game about rolling a box to a specific position on a special plane. Precisely, the pla ...

  6. JS同步执行代码

    new Promise(function(){initAppToken()}).then(()=>                     getApps(this.pageInfo).then ...

  7. JavaScript数组为什么是对象

    有过PHP编程经验的程序员学习JavaScript的时候,会发现数组也是对象,这和PHP是不同的,在PHP中数组就是数组类型,并不是是对象.究竟为什么在JavaScript中数组会是对象呢? var ...

  8. 详解Document.Cookie

    转自:https://www.jb51.net/article/77009.htm 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. 同时 ...

  9. python __str__repr__ 区别

    __str__ __repr__ 两个内置函数都是调试常用的函数, 对象直接调用时会调用 __repr__的内容, __str__需要print一下对象才可以 两个函数的内容有时会写成相同内容   _ ...

  10. linux NFS 客户端的安装

    1. 安装 showmount [root@allentuns ~]# yum -y install showmount 2. 查看服务器共享 [root@allentuns ~]# showmoun ...