【问题标题】:Postgres select record stored as epoch from yesterdayPostgres 选择从昨天开始存储为纪元的记录
【发布时间】:2018-04-26 09:08:57
【问题描述】:

我需要选择昨天的所有记录。表中的日期存储为纪元时间戳。我尝试了类似的方法,但它不起作用。

select to_char((SELECT TIMESTAMP WITH TIME ZONE 'epoch' + history.clock * INTERVAL '1 second') , 'YYYY-MM-DD HH24:MI:SS ') AS Data
from history
WHERE
history.clock >=  EXTRACT( epoch FROM current_timestamp - interval '1 day') and
history.clock <=  EXTRACT( epoch FROM current_timestamp - interval '1 day') ;

【问题讨论】:

  • “它不工作”是什么意思?引发错误?结果不正确?请使用示例数据和预期结果编辑您的问题。
  • 我没有给出任何结果。 Scoots 帮助 mi 现在。

标签: postgresql date epoch


【解决方案1】:

嗯,您的问题是您说的是“时钟 >= 1510444800 和时钟

以下查询应该满足您的需求。将范围的上限增加 86399(一天中有 86400 秒,但是当与 ::date 演员相结合时,我们已经从我们的提取中获得了当天的第一秒)

select
    to_char(to_timestamp(history.clock), 'YYYY-MM-DD HH24:MI:SS ') as Data
from
    history
where
    history.clock between extract(epoch from 'yesterday'::date)::integer and (extract(epoch from 'yesterday'::date)::integer + 86399)

我还包括了一些我认为可以改进的地方。当 Postgres 支持将'yesterday' 转换为日期时,无需为获取昨天的时间而烦恼。将值与上限和下限进行比较是 between 关键字的用途,并且有一个用于从 unix 时间戳转换的内置函数。

【讨论】:

  • 感谢我所需要的 :) 我看到 (extract(epoch from 'today'::date)::integer -1) 也给出了昨天的最后一秒
猜你喜欢
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 2019-12-05
  • 2020-12-02
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多