简体   繁体   English

Select 记录使用记录的列值作为间隔

[英]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.我希望能够根据列last_run获得所有“过期”的作业,这是一个时间戳,而interval_in_hours是一个 integer。 I saw from this post how to select records older than a static interval.我从这篇文章中看到如何 select 记录早于 static 间隔。 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.我可以添加一个跟踪类似next_run_time的列,并在每次执行作业时更新,尽管它是派生数据。 Is the performance significantly better for the latter approach?后一种方法的性能明显更好吗?

Use make_interval :使用make_interval

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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