源数据如下:
需要的效果是根据target_id分组取得最新的数据也就是:
一款基于PHP、MySQL、SNMP及RRDTool开发的网络流量监测图形分析工具,通过snmpget来获取数据,使用RRDtool绘画图形,提供了非常强大的数据和用户管理功能
82
目前想到两种方案:
方案一: 通过同表子查询或联查找到最大的数据ID
还有种写法:
<code>select * from
(select * from track
where type='task' and target_id in(...) ORDER BY time DESC
) as temp
GROUP BY target_id</code>方案二: 分两步查询,php中先查询最大ID,再通过ID数组查询列表数据
我想问的是有没有其它简单点的方法处理这种问题?
这种需求应该比较常见!
=====附上结构及数据=====
<code>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');</code>源数据如下:
需要的效果是根据target_id分组取得最新的数据也就是:
目前想到两种方案:
方案一: 通过同表子查询或联查找到最大的数据ID
还有种写法:
<code>select * from
(select * from track
where type='task' and target_id in(...) ORDER BY time DESC
) as temp
GROUP BY target_id</code>方案二: 分两步查询,php中先查询最大ID,再通过ID数组查询列表数据
我想问的是有没有其它简单点的方法处理这种问题?
这种需求应该比较常见!
=====附上结构及数据=====
<code>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');</code>
把最大id的数据放到临时表里
<code>CREATE TEMPORARY TABLE tmp_id(`id` int(11) not null,PRIMARY KEY (`id`) ) </code>
然后
<code>INSERT INTO tmp_id SELECT max(`id`) as id FROM track GROUP BY target_id </code>
然后join一下就可以了
最后还是建议显式删除临时表
<code>DROP TEMPORARY TABLE IF EXISTS tmp_id</code>
我觉得方案1A挺好了,不过为什么target_id还要写in条件呢,不写也是一样的结果。
一般都是用方案1,方案1还有另一种写法
<code>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);</code>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号