SELECT id, job_id, start_time, end_time, cost_time, STATUS, error_message FROM ( SELECT id, job_id, start_time, end_time, cost_time, STATUS, error_message, IF ( @jobid = job_id, @num :=@num + 1, @num := 1 ) AS num, @jobid := job_id FROM pybee_job_detail ORDER BY job_id, end_time DESC ) tab WHERE num = 1;