运算符不存在:interval > integer。

7
我有一个在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.

“Column modified 是时间戳(timestamp),timeout 是整数(integer)。
需要在服务器上更改一些设置吗?
我正在为客户在新服务器(Ubuntu)上安装应用程序,所以无法更改应用程序中的查询。”
3个回答

12

7.4和8.3之间有许多变化,其中一些最显著的是删除了一些自动转换。

我想“timeout”是以秒为单位的吧?如果是这样,您可以将查询更改为:

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

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;

ought to do it.


这个版本比pgsql版本更好,因为它不需要pgsql。我还运行了“乘法与1秒转换”和“连接到字符串并转换”的时间测试。这个版本(1秒转换)速度快约30%。 - reedstrm

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;

(假设超时时间以秒为单位 - 否则请相应更改)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接