首先,將按條件查詢並排序的結果查詢出來。
1 mysql> select accepttime,user,job from tuser_job where user =8 order by accepttime desc; 2 +---------------------+------+-----+ 3 | accepttime | user | job | 4 +---------------------+------+-----+ 5 | 2015-07-27 15:32:45 | 8 | 1 | 6 | 2015-07-26 14:22:56 | 8 | 1 | 7 | 2015-07-26 10:24:39 | 8 | 2 | 8 | 2015-07-20 17:16:38 | 8 | 1 | 9 +---------------------+------+-----+ 10 4 rows in set
然后,從中分組選出最新一條記錄。
1 mysql> select accepttime,user,job from (select * from tuser_job where user =8 order by accepttime desc) as a group by job; 2 +---------------------+------+-----+ 3 | accepttime | user | job | 4 +---------------------+------+-----+ 5 | 2015-07-27 15:32:45 | 8 | 1 | 6 | 2015-07-26 10:24:39 | 8 | 2 | 7 +---------------------+------+-----+ 8 2 rows in set
