简体   繁体   中英

Select records using records' column value as interval

I want to be able to get all the jobs that are "past due" based on the columns last_run , which is a timestamp and interval_in_hours which is an integer. I saw from this post how to select records older than a static interval. How can I use the value of one of the columns in the interval? Something like

-- ... | interval_in_hours | last_run --
SELECT * from jobs WHERE last_run < NOW() - INTERVAL '<interval_in_hours> hours'

I could add a column that tracks something like next_run_time and gets updated each time the job gets executed, though it is derived data. Is the performance significantly better for the latter approach?

Use make_interval :

SELECT * 
from jobs 
WHERE last_run < NOW() - make_interval(hours => interval_in_hours);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM