mysql取出每个分组中最新的记录
2017-04-11 15:41
411 查看
mysql的gruopby分组功能没有排序功能,所以我们如果想取出某个分组下的最新记录是不太容易的,下面介绍两种方法,一种是通过子查询,一种是通过group_concat函数来实现。
一、表结构及数据插入
#表的结构 `test3`
CREATE TABLE IF NOT EXISTS `test3` (
`id` int(11) NOT NULL auto_increment,
`bid` int(11) NOT NULL,
`cid` int(11) NOT NULL,
`dtime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
#转存表中的数据 `test3`
INSERT INTO `test3` (`id`, `bid`, `cid`, `dtime`) VALUES
(1, 1, 3, ’2014-03-18 16:00:00′),
(2, 1, 10, ’2014-03-18 17:00:00′),
(3, 2, 5, ’2014-03-18 18:00:00′),
(4, 2, 6, ’2014-03-18 19:00:00′),
(5, 1, 7, ’2014-03-18 20:00:00′);
二、通过子查询实现
1、sql语句
select * from(select * from test3 ORDER BY dtime DESC) as tempGROUP BY bid ORDER BY dtime DESC;
三、通过group_concat函数
1、完整的语法
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator'分隔符'])
作用:将要连接的字段按照排序字段的顺序用分隔符连起来显示,默认分隔符是”,”。
2、sql语句
select substring_index(group_concat(id order by `dtime`desc),’,',1) as id, substring_index(group_concat(bid order by`dtime` desc),’,',1) as bid,substring_index(group_concat(cid orderby `dtime` desc),’,',1) as cid,substring_index(group_concat(dtimeorder by `dtime`
desc),’,',1) as dtime from `test3` group bybid;
两上结果是一致的,虽然mysql自身的group by没有排序功能,但是通过自已的思考还是有办法的,写此作为备忘吧。
select * from (SELECT * FROM jcacard.v_inoroutrecord order by RecordDateTime desc) as temp
group by EmployName
having InOrOut = 1
order by RecordDateTime
一、表结构及数据插入
#表的结构 `test3`
CREATE TABLE IF NOT EXISTS `test3` (
`id` int(11) NOT NULL auto_increment,
`bid` int(11) NOT NULL,
`cid` int(11) NOT NULL,
`dtime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
#转存表中的数据 `test3`
INSERT INTO `test3` (`id`, `bid`, `cid`, `dtime`) VALUES
(1, 1, 3, ’2014-03-18 16:00:00′),
(2, 1, 10, ’2014-03-18 17:00:00′),
(3, 2, 5, ’2014-03-18 18:00:00′),
(4, 2, 6, ’2014-03-18 19:00:00′),
(5, 1, 7, ’2014-03-18 20:00:00′);
二、通过子查询实现
1、sql语句
select * from(select * from test3 ORDER BY dtime DESC) as tempGROUP BY bid ORDER BY dtime DESC;
三、通过group_concat函数
1、完整的语法
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator'分隔符'])
作用:将要连接的字段按照排序字段的顺序用分隔符连起来显示,默认分隔符是”,”。
2、sql语句
select substring_index(group_concat(id order by `dtime`desc),’,',1) as id, substring_index(group_concat(bid order by`dtime` desc),’,',1) as bid,substring_index(group_concat(cid orderby `dtime` desc),’,',1) as cid,substring_index(group_concat(dtimeorder by `dtime`
desc),’,',1) as dtime from `test3` group bybid;
两上结果是一致的,虽然mysql自身的group by没有排序功能,但是通过自已的思考还是有办法的,写此作为备忘吧。
select * from (SELECT * FROM jcacard.v_inoroutrecord order by RecordDateTime desc) as temp
group by EmployName
having InOrOut = 1
order by RecordDateTime
相关文章推荐
- mysql取出每个分组中最新的记录
- [MySQL]学习笔记- 用户行为表中,查询每个人的一条最新行为(分组 排序 取时间最大的一条记录)
- MySql中取出每个分组中的前N条记录
- mysql 分组取最新的一条记录(整条记录)
- mysql 分组取最新的一条记录(整条记录) 解决我困扰好多天的问题
- 【记录】T-SQL 分组排序中取出最新数据
- mysql 取每个分组中时间最近的记录
- mysql 分组取最新的一条记录(整条记录)
- MySQL分组查询获取每个学生前n条分数记录(分组查询前n条记录)
- oracle取出每个分组的第一条记录
- mysql分组后获得每个分组记录数
- mysql 读取某个分组最大值,或最新记录的sql
- mysql 分组后取每个组内最新的一条数据
- mysql分组查询取分组后各分组中的最新一条记录
- 如何取出每个分组的第一条记录
- 4000 mysql分组排序取最大、最小、最新、前N条记录
- yii 使用原生sql 查询 以某一字段分组 每个字段取出最新的一条数据
- mysql 分组取最新的一条记录(整条记录)
- 【记录】T-SQL 分组排序中取出最新数据