首页 > 技术文章 > sql分组排序取top

zhoujl-5071 2018-12-14 18:49 原文

写法1:

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:

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效率更高!

推荐阅读