mysql獲取按日期排序獲取最新的記錄
阿新 • • 發佈:2019-01-10
主要按照日期獲得最新的資料;
今天記錄兩種方式,並不涉及效率等其他方面問題:
第一種, 利用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