![sql分组排序取top sql分组排序取top](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
写法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效率更高!