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

时间:2012-07-02 13:49:55

标签: postgresql postgresql-8.4

我需要添加来自整数列的分钟,并使用时间戳与另一列进行比较。

这是一个例子:

 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.periodicidade是整数(分钟)
我需要验证data_extracao(timestamp)是否大于NOW() + l.periodicidade(integer - minutes)

我该怎么做?

1 个答案:

答案 0 :(得分:22)

您可以这样编写查询:

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)

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

希望有所帮助