mysql 分组后取每个组内最新的一条数据

首先,将按条件查询并排序的结果查询出来。

 mysql> select accepttime,user,job from tuser_job where user =8  order by accepttime desc;
+---------------------+------+-----+
| accepttime | user | job |
+---------------------+------+-----+
| 2015-07-27 15:32:45 | 8 | 1 |
| 2015-07-26 14:22:56 | 8 | 1 |
| 2015-07-26 10:24:39 | 8 | 2 |
| 2015-07-20 17:16:38 | 8 | 1 |
+---------------------+------+-----+
4 rows in set

然后,从中分组选出最新一条记录。

 mysql> select accepttime,user,job from (select * from tuser_job where user =8  order by accepttime desc) as a group by job;
+---------------------+------+-----+
| accepttime | user | job |
+---------------------+------+-----+
| 2015-07-27 15:32:45 | 8 | 1 |
| 2015-07-26 10:24:39 | 8 | 2 |
+---------------------+------+-----+
2 rows in set
上一篇:Linux 下 swap 分区及作用详解


下一篇:Win7 64位 安装E10后 打不开的解决方案 -摘自网络