mysql 分組后取每個組內最新的一條數據


首先,將按條件查詢並排序的結果查詢出來。

 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM