Sql 我可以用模来指定postgres中的间隔吗?
我必须列出在上次更新后两年多次未更新的项目。这是作为cron作业每天运行一次 我知道我可以做一些丑陋的事情,比如:Sql 我可以用模来指定postgres中的间隔吗?,sql,postgresql,Sql,Postgresql,我必须列出在上次更新后两年多次未更新的项目。这是作为cron作业每天运行一次 我知道我可以做一些丑陋的事情,比如: SELECT art_id, art_update FROM items WHERE art_update = now()::date - interval '2 years' OR art_update = now()::date - interval '4 years' OR art_update = now()::date - interval '6 years' OR a
SELECT art_id, art_update FROM items
WHERE art_update = now()::date - interval '2 years'
OR art_update = now()::date - interval '4 years'
OR art_update = now()::date - interval '6 years'
OR art_update = now()::date - interval '8 years'
OR art_update = now()::date - interval '10 years';
有没有办法通过检查模间隔来避免这种情况?或者用其他一般的方式来表达
select art_id, art_update
from items
where art_update in (
(now() - interval '2 years')::date,
(now() - interval '4 years')::date,
(now() - interval '6 years')::date,
(now() - interval '8 years')::date,
(now() - interval '10 years')::date
);
或
您可以每隔2年生成一系列日期,这些日期可以追溯到今天(以下为10年前),并将其连接到您的表中:
SELECT i.art_id, i.art_update
FROM items i
INNER JOIN generate_series(2, 10, 2) s (years)
ON i.art_update = now()::date - interval '1 years' * s.years;
注意:如果您在序列中生成日期,而不是数字,则这似乎稍微快一些:
SELECT i.art_id, i.art_update
FROM items i
INNER JOIN generate_series(now() - interval '10 years',
now() - interval '2 years',
interval '2 years') d (d)
ON art_update = d.d::date;
你可以试试这个
SELECT art_id, art_update
FROM items
Where int4(date_part('year', art_update)) % 2 = 0;
这将在2004年、2006年、2008年、2010年和2012年每天重现。不仅仅是今天(例如2010-03-06),这是非常正确的,谢谢。我选择这个而不是上面Clodoaldo Neto的答案,因为我更喜欢联接而不是子查询。非常感谢你的帮助。
SELECT art_id, art_update
FROM items
Where int4(date_part('year', art_update)) % 2 = 0;