A为任务表,B为日志表。一个任务可能有多个日志,一次任务调度就是一个日志;
现在想要拿到最新一次的调度;也就是单个任务的最新一次调度的信息;
SELECT *
from (
select info.* from xxl_job_info info inner join
(
SELECT
t1.*
FROM
xxl_job_log AS t1
INNER JOIN (
SELECT
job_id,
MAX(trigger_time) AS max_create_time
FROM
xxl_job_log
GROUP BY
job_id
) AS t2 ON t1.job_id = t2.job_id
AND t1.trigger_time = t2.max_create_time
)
log on info.trigger_status = 1 and info.author ="renwu"
and
info.job_group =3 and info.id = log.job_id and
log.trigger_code in (0, 200) and log.handle_code = 0
) t
其中最重要的是下面的逻辑,根据任务id分组,获取每一个任务id的最新的记录;按照时间进行排序,最近时间的日志数据查询出来,就是根据下面的sql语句进行查询;
SELECT
t1.*
FROM
xxl_job_log AS t1
INNER JOIN (
SELECT
job_id,
MAX(trigger_time) AS max_create_time
FROM
xxl_job_log
GROUP BY
job_id
) AS t2 ON t1.job_id = t2.job_id
AND t1.trigger_time = t2.max_create_time
)