postgresql – 在Postgres中添加current_timestamp和days列的总
发布时间:2020-12-13 15:49:29 所属栏目:百科 来源:网络整理
导读:我希望通过将天数添加到当前时间来更新列.在pseudosyntax中它将是: UPDATE fooSET time = current_timestamp + days::integer days是同一个表中的一列. 解决方法 create function add_days_to_timestamp(t timestamptz,d int) returns timestamptzas$$begin
我希望通过将天数添加到当前时间来更新列.在pseudosyntax中它将是:
UPDATE foo SET time = current_timestamp + days::integer days是同一个表中的一列. 解决方法create function add_days_to_timestamp(t timestamptz,d int) returns timestamptz as $$ begin return t + interval '1' day * d; end; $$language 'plpgsql'; create operator + (leftarg = timestamptz,rightarg = int,procedure = add_days_to_timestamp); 现在这可行: update foo set time = current_timestamp + 3 /* day variable here,or a column from your table */ 注意: 出于某种原因,在Postgres中内置了一个到目前为止的整数,这可以工作: select current_timestamp::date + 3 -- but only a date 这不会(除非你定义自己的运算符,见上文): select current_timestamp + 3 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |