mysql 如何获取每一组创建时间最小的记录
来源:互联网 发布:融学软件下载 编辑:程序博客网 时间:2024/05/29 17:21
需求:要根据每种店铺类型获取到创建时间最早的那条记录
表结构如下:
CREATE TABLE `finance_rent_mode_dealer` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', `dealerid` int(11) NOT NULL DEFAULT '0' COMMENT '商家ID', `type` tinyint(3) NOT NULL DEFAULT '0' COMMENT '类型 1,自营;2,4S店铺;', `rent_price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '直租价格(万元)', `modeid` int(11) NOT NULL DEFAULT '0' COMMENT '车型ID', `createid` int(11) NOT NULL DEFAULT '0' COMMENT '创建人ID', `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updateid` int(11) NOT NULL DEFAULT '0' COMMENT '修改人', `updatetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', `status` tinyint(3) NOT NULL DEFAULT '0' COMMENT '状态 1,生效;0,失效;', `seriesid` int(11) NOT NULL DEFAULT '0' COMMENT '车系ID', `brandid` int(11) NOT NULL DEFAULT '0' COMMENT '品牌ID', `rent_status` tinyint(3) NOT NULL DEFAULT '-1' COMMENT '直租状态 (-2=车型亮点无效,-1=直租方案无效,1=上架,2=下架)', PRIMARY KEY (`id`), KEY `idx_dealerid` (`dealerid`), KEY `idx_modeid` (`modeid`,`status`,`type`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='金融商家车型关联表(直租)';
实现该需求有两种方案
第一种方案:先排序后分组
select * from (select a1.modeid,a1.type,a1.createtime,a1.dealerid,status,rent_status,brandid,a1.seriesid from finance_rent_mode_dealer a1 where a1.status=1 and a1.rent_status=1 order by a1.modeid desc,a1.type desc, a1.createtime asc) agroup by a.modeid desc,a.type desc order by null;
### 先排好序,然后分组的时候会自动获取到第一条数据,数据量会多一些
第二种方案:先分组后排序
select a1.modeid,a1.type,substring_index(group_concat(dealerid order by createtime asc),',',1) dealerid,status,rent_statusfrom finance_rent_mode_dealer a1 where a1.status=1 and a1.rent_status=1 group by a1.modeid desc,a1.type desc;
### 分组的过程实际上就已经有序了,而且不用嵌套就能获取到想要的结果,主要是用group_contact语句
最终结果对比:
select @@profiling;##set profiling=1;###测试接下来两句的性能select sql_no_cache modeid,type,substring_index(group_concat(dealerid order by createtime asc),',',1) dealeridfrom finance_rent_mode_dealer where status=1 and rent_status=1 group by modeid desc,type desc;select sql_no_cache * from (select a1.modeid,a1.type,a1.createtime,a1.dealerid,status,rent_status,brandid,a1.seriesid from finance_rent_mode_dealer a1 where a1.status=1 and a1.rent_status=1 order by a1.modeid desc,a1.type desc, a1.createtime asc) agroup by a.modeid desc,a.type desc order by null;show profiles; ###显示出每一个sql语句的执行时间
最终结果:
从结果上可以看出,因为先分组的结果使用了大量的函数,导致性能反而没有先排序后分组的优秀,看来sql语句中使用函数是不可取的。
阅读全文
0 0
- mysql 如何获取每一组创建时间最小的记录
- 从一组时间列表中获取一组最近的时间
- MSSQL分组取后每一组的最新一条记录
- 如何获取目录的创建时间 - Delphi /
- MySQL中日期类型数据的使用:如何让字段保存记录的创建时间和最后修改时间
- mysql获取从某个时间到今天的每一天的日期,以及笛卡尔积
- secureCRT 日志保存并记录每条记录的时间
- Mysql获取每组前N条记录
- java list<integer>一组不连续的数字进行每10个一组分组,要求各组元素之和差值最小。list%10>=5的单独算一组,否则归并到任意一组。
- linq 分组 ,每组取时间最新的记录
- 给每条记录的时间增加随机秒数
- Java如何获取所查询的结果集的列数,并将每条记录打印出来
- 获取每组最新记录
- Mysql中如何记录某个User创建的日期
- 如何求某个分组里面的最小时间点对应的那条记录
- js获取每一天24点的时间
- MYSQL中如何 高效获取新插入的流水记录
- mysql 取每组前几条记录
- 论文笔记:第一人称视角视频中的行人轨迹预测
- React + MobX 入门及实例(二)
- IntelliJ IDEA使用手册——远程调试
- 机房之上机窗体
- jconsole远程连接配置
- mysql 如何获取每一组创建时间最小的记录
- Dubbo服务集群,常见容错机制:failover ,failsafe,failfase ,failback,forking
- Mybatis分页处理
- 深入浅出: Java回调机制(异步)
- SYSU Matrix上的选做题——多柱汉诺塔
- poj 3190
- 自定义Dialog从底部滑入,滑出。
- 【网络流二十四题 魔术球问题】【DAG 最小路径覆盖->最大流】【灵感】
- python爬虫学习 之 定向爬取 淘宝商品价格