mysql 分组取最新的一条记录(整条记录)

2014-05-05 13:35

   mysql取分组后最新的一条记录,下面两种方法.一种是先筛选 出最大和最新的时间,在连表查询.一种是先排序,然后在次分组查询(默认第一条),就是最新的一条数据了  


[plain] view plaincopy


   #select * from t_assistant_article as a, (select max(base_id) as base_id, max(create_time) as create_time from t_assistant_article as b group by base_id ) as b where a.base_id=b.base_id and a.create_time = b.create_time  

   #select base_id,max(create_time), max(article_id) as article_id from t_assistant_article as b group by base_id  

   select * from (select * from t_assistant_article order by create_time desc) as a group by base_id  

^