主要按照日期获得最新的数据;
今天记录两种方式,并不涉及效率等其他方面问题:
第一种, 利用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