【问题标题】:Last 7 days from postgres table epoch timestamp从 postgres 表纪元时间戳开始的最后 7 天
【发布时间】:2019-12-05 09:58:32
【问题描述】:

我有一个表(funny_quotes),它有一个名为 quote_date 的列,它以 unix 纪元时间(秒)存储数据

我想运行一个查询,只返回该表(funny_quotes)中最近 3 天的数据。

我在 postgres 中本地创建了一个表,但无法将日期存储为纪元时间,它们只能存储为时间戳值

select * from funny_quotes where quote_date > (now() - interval '3 days')

【问题讨论】:

  • 3 天还是 7 天?请显示表定义。
  • @fphilipe - 3 天,它是一个简单的表,有 2 列,只有一个称为 varchar 类型的引号和 bigint 类型的 quote_date
  • 您是否有遗留代码需要使用 epoch 进行维护?如果不是,为什么在 Postgres 具有处理时间戳的广泛功能时选择它。 Unix 纪元时间非常适合 shell 脚本,但并非如此。如果您必须维护遗留代码考虑添加时间戳列(是的,保持同步很痛苦),但是当版本 12 到来时(现在处于 beta 2 中),可以将其更改为“生成的列”。

标签: postgresql


【解决方案1】:

您应该将等式的右侧修改为 epoch 以便能够进行比较:

select * from funny_quotes where quote_date > extract(epoch from (now() - interval '3 days'))

或者反过来:将quote_date 转换为时间戳:

select * from funny_quotes where to_timestamp(quote_date) > now() - interval '3 days'

您可以阅读更多关于它的信息in the docs

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 2011-03-24
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2012-03-12
    • 1970-01-01
    相关资源
    最近更新 更多