我已经看过很多与此相关的问题,但我似乎无法解决任何问题。我不太擅长半复杂的 SQL 查询。
我想获取当前时间与unix时间戳中的列(以小时为单位)之间的差异。
我不确定我在这件事上做错了什么或做对了。目标是仅提取 24 小时以内的行。如果有更好的方法或例子,那就太好了。 我从这里尝试了几个答案
PostgreSQL 的时间戳差异wc.posted
是一家
bigint
商店,为 unix timestamp
SELECT w.wal_id, wc.com_id, w.posted AS post_time, wc.posted AS com_time
FROM wall as w LEFT JOIN wall_comments as wc ON w.wal_id=wc.wal_id
WHERE (EXTRACT(EPOCH FROM wc.posted)) > current_timestamp - interval '24 hours'
然后错误:
ERROR: function pg_catalog.date_part(unknown, bigint) does not exist
LINE 1: ... wall_comments as wc ON w.wal_id=wc.wal_id WHERE (EXTRACT(EP...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function pg_catalog.date_part(unknown, bigint) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 148
这是一个简化的小提琴
to_timestamp
函数;它接受
参数并从 Unix 纪元(自 1970-01-01 00:00:00+00 以来的秒数)转换为double precision
。 (timestamp with time zone
Unix 纪元隐式转换为Integer
。) 因此,要将您的double precision
bigint
自纪元以来的秒数转换为
timestampz
:to_timestamp(wc.posted)
也许您正在寻找这个:
WHERE to_timestamp(wc.posted) > current_timestamp - interval '24 hours'
select field_1,field_2,field_3 from schema_name.table_name Where
ROUND(EXTRACT(EPOCH FROM (cast(now() as timestamp) - cast(your_time_field as timestamp)))/60) > 1440;
提示:没有函数与给定的名称和参数类型匹配。您可能需要添加显式类型转换。
添加显式时间戳类型转换事实上,最简单的解决方案是
。
SELECT w.wal_id, wc.com_id, w.posted AS post_time, wc.posted AS com_time
FROM wall as w LEFT JOIN wall_comments as wc ON w.wal_id=wc.wal_id
WHERE (EXTRACT(EPOCH FROM wc.posted::timestamp)) > current_timestamp - interval '24 hours'
注意时间戳类型转换:
wc.posted::timestamp