Postgres select 记录从昨天开始存储为纪元
Postgres select record stored as epoch from yesterday
我需要 select 昨天的所有记录。 table 中的日期存储为纪元时间戳。我尝试过类似的方法,但它不起作用。
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') ;
嗯,你的问题是你在说 "everything where clock >= 1510444800 and clock <= 1510444800"。你给了自己一个秒范围。
以下查询应该可以满足您的需要。将范围上限增加 86399(一天有 86400 秒,但是当与 ::date
cast 相结合时,我们已经从我们的提取中获得了一天的第一秒)
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 时间戳转换的内置函数。
我需要 select 昨天的所有记录。 table 中的日期存储为纪元时间戳。我尝试过类似的方法,但它不起作用。
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') ;
嗯,你的问题是你在说 "everything where clock >= 1510444800 and clock <= 1510444800"。你给了自己一个秒范围。
以下查询应该可以满足您的需要。将范围上限增加 86399(一天有 86400 秒,但是当与 ::date
cast 相结合时,我们已经从我们的提取中获得了一天的第一秒)
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 时间戳转换的内置函数。