mysql获取按日期排序获取最新的记录

主要按照日期获得最新的数据;

今天记录两种方式,并不涉及效率等其他方面问题:

第一种, 利用GROUP BY原理:

select * from (
	select * from `authenticationrecord` order by `authenticationtime` desc
) `temp`  group by merchantrid order by `authenticationtime` desc

第二种,利用mysql内置函数

select * from `test` where id in(select SUBSTRING_INDEX(group_concat(id order by `date` desc),',',1) from `test` group by category_id ) order by `date` desc


<<<<<<<内容完<><><>

试验脚本(自己玩):

create table STAFF(
	id  int,
	sname  varchar(50),
dept  varchar(50),
salary  int,
edlevel  int,
hiredate datetime
)

insert into STAFF values('1','张三','开发部','2000','3','2009-10-11');
insert into STAFF values('2','李四','开发部','2500','3','2009-10-01');
insert into STAFF values('3','王五','设计部','2600','5','2010-10-02');
insert into STAFF values('4','王六','设计部','2300','4','2010-10-03');
insert into STAFF values('5','马七','设计部','2100','4','2010-10-06');
insert into STAFF values('6','赵八','销售部','3000','5','2010-10-05');
insert into STAFF values('7','钱九','销售部','3100','7','2010-10-07');
insert into STAFF values('8','孙十','销售部','3500','7','2010-10-06');


select group_concat(id order by hiredate desc) as test from staff
group by dept;

select * from STAFF;


select * from STAFF
group by dept



参考网站:http://blog.csdn.net/agileclipse/article/details/12184635




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值