sql分組排序取top

寫法1:spa

use anypay;
select tr.* from (select task_code, max(created_at) as cal from task_log group by task_code ) tl join task_log tr on tl.task_code = tr.task_code and tl.cal = tr.created_at;

寫法2:code

use anypay;
SELECT * FROM task_log AS t1 WHERE created_at = (SELECT MAX(created_at) FROM task_log AS t2 WHERE t1.task_code = t2.task_code);

寫法2效率更高!blog

相關文章
相關標籤/搜索