首先,将按条件查询并排序的结果查询出来。
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