Given the following information, how can I select the most recent line items (based on time_entered
) on unique params
and cron_action_id
pairs that haven't been executed?
cron_s开发者_如何转开发chedule
For example, ids 1, 2, and 4 have the same params
and cron_action_id
, so I need not select all 3, just id 4. Same principle for id 3/5 and 7/8.
I can only get so far as
SELECT *
FROM cron_schedule cs
INNER JOIN cron_actions ca
ON cs.cron_action_id = ca.cron_action_id
WHERE time_executed = 0
-- GROUP BY (params, cron_action_id) ?
This should return rows with id 4, 5, 6, and 8
Thanks
SELECT t1.*
FROM cron_schedule t1
INNER JOIN (SELECT params,
cs.cron_action_id,
Max(time_entered) AS time_entered
FROM cron_schedule cs
INNER JOIN cron_actions ca
ON cs.cron_action_id = ca.cron_action_id
WHERE time_executed = 0
GROUP BY params, cron_action_id) AS t2
ON t1.params = t2.params
AND t1.cron_action_id = t2.cron_action_id
AND t1.time_entered = t2.time_entered
INNER JOIN cron_actions ca2
ON t1.cron_action_id = ca2.cron_action_id
WHERE t1.time_executed = 0
Not tested, but this basic idea should work:
SELECT *
FROM
cron_schedule outer_cron_schedule JOIN cron_actions
WHERE
time_entered = (
SELECT MAX(time_entered)
FROM cron_schedule inner_cron_schedule
WHERE
outer_cron_schedule.id = inner_cron_schedule.id
GROUP BY
params, cron_action_id
)
AND time_executed = 0
Give it a try
精彩评论