在 PostgreSQL 中为 CURRENT_TIMESTAMP 添加分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21745125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add minutes to CURRENT_TIMESTAMP in PostgreSQL
提问by OneCart
I have a procedure in PostgreSQL that I want to add the number of minutes to CURRENT_TIMESTAMP like below
我在 PostgreSQL 中有一个程序,我想将分钟数添加到 CURRENT_TIMESTAMP 中,如下所示
timestamp_var := CURRENT_TIMESTAMP + interval '20 minutes';
timestamp_var := CURRENT_TIMESTAMP + 间隔“20 分钟”;
But the number of minutes is a parameter.
但分钟数是一个参数。
Do we have the functions to do this?
我们是否有能力做到这一点?
Pls help me in this case
在这种情况下请帮助我
CREATE OR REPLACE FUNCTION modify_time(id users.id%TYPE, min integer) AS $$
BEGIN
UPDATE
users
SET
modified_at = CURRENT_TIMESTAMP
WHERE
user_id = id;
END
$$ LANGUAGE plpgsql;
I want to add minminutes to CURRENT_TIMESTAMP thanks
我想补充分钟分钟CURRENT_TIMESTAMP谢谢
回答by stv
You can multiply intervals by integers. The following gives you a timestamp 20 minutes in the future:
您可以将间隔乘以整数。以下为您提供了未来 20 分钟的时间戳:
select current_timestamp + (20 * interval '1 minute')
Or, as murisonmentions in another answerto this question, there is a more succinct way to express this:
或者,正如穆里森在这个问题的另一个答案中提到的,有一种更简洁的方式来表达这一点:
select current_timestamp + (20 ||' minutes')::interval
So, your code could look like:
因此,您的代码可能如下所示:
CREATE OR REPLACE FUNCTION modify_time(id users.id%TYPE, min integer) AS $$
BEGIN
UPDATE
users
SET
modified_at = CURRENT_TIMESTAMP + (min * interval '1 minute')
WHERE
user_id = id;
END
$$ LANGUAGE plpgsql;
回答by murison
the other way is
另一种方式是
select current_timestamp + (20 ||' minutes')::interval