以下是当前查询:
select * from callhistory where created>date_trunc('minute', timestamp (current_timestamp-interval '5' minute));
它不返回任何东西。此外,创建的字段的格式为“2011-05-18 18:11:32.024。”
任何帮助都将不胜感激。
答案 0 :(得分:2)
语法是你的date_trunc
有点偏离:
select *
from callhistory
where created > date_trunc('minute', current_timestamp - interval '5' minute)
您也可以使用now()
in place of current_timestamp
:
select *
from callhistory
where created > date_trunc('minute', now() - interval '5' minute)
一个例子:
=> create table stuff (created timestamp not null);
=> insert into stuff (created) values (current_timestamp);
-- Wait a could seconds...
=> insert into stuff (created) values (current_timestamp);
=> select * from stuff where created > date_trunc('minute', current_timestamp - interval '5' minute);
created
----------------------------
2011-06-01 11:32:55.672027
2011-06-01 11:33:00.182953
=> select * from stuff where created > date_trunc('minute', current_timestamp - interval '7' day);
created
----------------------------
2011-06-01 11:32:55.672027
2011-06-01 11:33:00.182953
更新:看起来PostgreSQL版本8对interval
的格式稍微严格,fine manual表示您应该使用interval '$n $unit'
其中$n
1}}是事物的数量,$unit
是时间单位。版本9允许您在没有投诉的情况下说interval '$n' $unit
,但如果您不使用文档格式,版本8会将您的间隔转换为0。因此,您应该将它用于版本8和版本9:
select *
from callhistory
where created > date_trunc('minute', current_timestamp - interval '5 minute')
答案 1 :(得分:0)
这是我对PostgreSQL 8.3服务器进行的示例测试。你没有提到你正在运行的版本。
select current_timestamp, to_char((current_timestamp - '5 minutes'::interval), 'yyyy-mm-dd HH:mi:00')::timestamp;
间隔扣除5分钟,to_char()方法向下舍入到最接近的分钟。如果这是您要查找的内容,则查询应如下所示:
select * from callhistory where created > to_char((current_timestamp - '5 minutes'::interval), 'yyyy-mm-dd HH:mi:00')::timestamp