运算符不存在:interval>整数

时间:2008-11-14 14:30:12

标签: sql postgresql

我有一个适用于Postgresql 7.4但不适用于Postgresql 8.3且具有相同数据库的查询。

查询:

SELECT * FROM login_session WHERE (now()-modified) > timeout;

获取以下错误:

ERROR:  operator does not exist: interval > integer
LINE 1: ...ELECT * FROM login_session WHERE (now()-modified) > timeout ...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

modifiedtimestamptimeoutinteger

我需要在服务器上更改一些设置吗?

我正在新服务器(ubuntu)上为客户端安装应用程序,因此我无法在应用程序中更改查询。

3 个答案:

答案 0 :(得分:7)

7.4和8.3之间有很多变化。一些最激烈的是删除一些自动演员。

我想“超时”是几秒钟?如果是这样,您可以将查询更改为:

SELECT
    *
FROM
    login_session
WHERE
    (CURRENT_TIMESTAMP - modified) > (timeout * '1 sec'::interval);

答案 1 :(得分:3)

create or replace function int2interval (x integer) returns interval as $$ select $1*'1 sec'::interval $$ language sql;
create cast (integer as interval) with function int2interval (integer) as implicit;

应该这样做。

答案 2 :(得分:1)

CREATE OR REPLACE FUNCTION intToInterval(arg integer)
  RETURNS interval AS
$BODY$
   BEGIN      
      return CAST( arg || ' seconds' AS interval ); 
   END;
$BODY$
  LANGUAGE 'plpgsql';

CREATE CAST (integer AS interval)
WITH FUNCTION intToInterval ( integer )
AS IMPLICIT;

(假设超时是以秒为单位测量的 - 否则会相应改变)

相关问题