跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

Mysql获取分组最新数据

源数据如下: 需要的效果是根据target_id分组取得最新的数据也就是: PHP 8.5.5 PHP 8.5.5 是 PHP 8.5 分支的维护更新版本。该版本延续了“小步快跑”的迭代逻辑,通过深度错误修复、底层性能微调以及安全加固,旨在为开发者提供一个更健壮、更高效的运行环境。该版本严格遵守语义化版本规范,不包含破坏性变更。 下载 目前想到两种方案: 方案一: 通过同表子查询或联查找到最大的数据ID 还有种写法:
select * from (select * from track where type='task' and target_id in(...) ORDER BY time DESC ) as temp GROUP BY target_id
方案二: 分两步查询, php 中先查询最大ID,再通过ID数组查询列表数据 我想问的是有没有其它简单点的方法处理这种问题? 这种需求应该比较常见! =====附上结构及数据=====
DROP TABLE IF EXISTS `track`; CREATE TABLE `track` ( `id` int(11) NOT NULL AUTO_INCREMENT, `type` varchar(50) NOT NULL DEFAULT '' COMMENT 'task => 任务跟进,project => 项目跟进 ', `target_id` int(11) DEFAULT '0' COMMENT '跟进目标ID', `user_id` int(11) DEFAULT '0' COMMENT '跟进用户', `user_name` varchar(100) DEFAULT '' COMMENT '跟进用户名称', `content` varchar(500) DEFAULT '' COMMENT '跟进内容', `time` int(11) DEFAULT '0' COMMENT '跟进时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COMMENT='跟进记录表'; -- ---------------------------- -- Records of track -- ---------------------------- INSERT INTO `track` VALUES ('1', 'task', '67', '1', '超级管理员', '无所谓...', '1467774850'); INSERT INTO `track` VALUES ('2', 'task', '67', '1', '超级管理员', 'TTTT', '1467777620'); INSERT INTO `track` VALUES ('7', 'task', '67', '1', '超级管理员', '只耗损', '1468288894'); INSERT INTO `track` VALUES ('8', 'task', '34', '1', '超级管理员', 'STS', '1468288917'); INSERT INTO `track` VALUES ('9', 'task', '34', '1', '超级管理员', '吊顶', '1468288954');
回复内容: 源数据如下: 需要的效果是根据target_id分组取得最新的数据也就是: 目前想到两种方案: 方案一: 通过同表子查询或联查找到最大的数据ID 还有种写法:
select * from (select * from track where type='task' and target_id in(...) ORDER BY time DESC ) as temp GROUP BY target_id
方案二: 分两步查询,php中先查询最大ID,再通过ID数组查询列表数据 我想问的是有没有其它简单点的方法处理这种问题? 这种需求应该比较常见! =====附上结构及数据=====
DROP TABLE IF EXISTS `track`; CREATE TABLE `track` ( `id` int(11) NOT NULL AUTO_INCREMENT, `type` varchar(50) NOT NULL DEFAULT '' COMMENT 'task => 任务跟进,project => 项目跟进 ', `target_id` int(11) DEFAULT '0' COMMENT '跟进目标ID', `user_id` int(11) DEFAULT '0' COMMENT '跟进用户', `user_name` varchar(100) DEFAULT '' COMMENT '跟进用户名称', `content` varchar(500) DEFAULT '' COMMENT '跟进内容', `time` int(11) DEFAULT '0' COMMENT '跟进时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COMMENT='跟进记录表'; -- ---------------------------- -- Records of track -- ---------------------------- INSERT INTO `track` VALUES ('1', 'task', '67', '1', '超级管理员', '无所谓...', '1467774850'); INSERT INTO `track` VALUES ('2', 'task', '67', '1', '超级管理员', 'TTTT', '1467777620'); INSERT INTO `track` VALUES ('7', 'task', '67', '1', '超级管理员', '只耗损', '1468288894'); INSERT INTO `track` VALUES ('8', 'task', '34', '1', '超级管理员', 'STS', '1468288917'); INSERT INTO `track` VALUES ('9', 'task', '34', '1', '超级管理员', '吊顶', '1468288954');
把最大id的数据放到临时表里
CREATE TEMPORARY TABLE tmp_id(`id` int(11) not null,PRIMARY KEY (`id`) )
然后
INSERT INTO tmp_id SELECT max(`id`) as id FROM track GROUP BY target_id
然后join一下就可以了 最后还是建议显式删除临时表
DROP TEMPORARY TABLE IF EXISTS tmp_id
我觉得方案1A挺好了,不过为什么target_id还要写in条件呢,不写也是一样的结果。 一般都是用方案1,方案1还有另一种写法
select * from track where id in(select substring_index(group_concat(id order by id desc),',',1) as maxid from track group by target_id);

相关文章