sql分组排序取top

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

猜你喜欢

转载自www.cnblogs.com/zhoujl-5071/p/10121002.html