PostgreSQL:从列值向时间戳添加间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11295045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:03:15  来源:igfitidea点击:

PostgreSQL: Add Interval to Timestamp from column value

postgresqlpostgresql-8.4

提问by NakaBr

I need to add minutes coming from an integer column with a timestamp to compare to another column.

我需要添加来自带有时间戳的整数列的分钟以与另一列进行比较。

Here's an example:

下面是一个例子:

 SELECT t1.id_liame, t1.id_table, t1.periodicidade , t3.data_extracao, 
    CASE WHEN(NOW() < (e.data_extracao + INTERVAL t1.periodicidade || '
    MINUTES')) 
    THEN 'yes' ELSE 'no' END
    FROM table1 as t1 LEFT JOIN liame_has_extracao as t2 USING(id_liame)
    LEFT JOIN extracao as t3 USING(id_extracao)

l.periodicidadeis integer (minutes)
I need to verify if data_extracao(timestamp)is greater then NOW() + l.periodicidade(integer - minutes).

l.periodicidade是整数(分钟)
我需要验证是否data_extracao(timestamp)大于 then NOW() + l.periodicidade(integer - minutes)

How can i do it?

我该怎么做?

回答by Frank Bollack

You can write your query like this:

您可以像这样编写查询:

SELECT 
   t1.id_liame,
   t1.id_table,
   t1.periodicidade,
   t3.data_extracao,
   CASE
      WHEN(NOW() < (t3.data_extracao + (INTERVAL '1 min' * t1.periodicidade))) 
      THEN 'yes' ELSE 'no'
   END
FROM table1 AS t1
LEFT JOIN liame_has_extracao AS t2 USING(id_liame)
LEFT JOIN extracao AS t3 USING(id_extracao)

As you can see, you can multiply intervals with integers so with INTERVAL '1 minute'you define your base unit and multiply your actual time interval.

如您所见,您可以将间隔与整数相乘,以便INTERVAL '1 minute'定义基本单位并乘以实际时间间隔。

Hope that helps

希望有帮助