源数据如下:
需要的效果是根据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');
回复内容: 源数据如下:
需要的效果是根据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);