Postgres INTERVAL使用表中的值 [英] Postgres INTERVAL using value from table

查看:367
本文介绍了Postgres INTERVAL使用表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在日期上增加5天,可以使用 INTERVAL 函数完成:

If I want to add 5 days to a date, I can do it using the INTERVAL function:

select create_ts + interval '5 days' from abc_company;

但是,我的表中有一个名为 num_of_days ,然后将其添加到我的create_ts中。像这样:

However, my table has a field called num_of_days and I want to add it to my create_ts. Something like this:

select create_ts + interval num_of_days || ' days' from abc_company;

这不起作用。

推荐答案

简单地将值与时间间隔相乘:

Simply multiply the value with an interval:

select create_ts + num_of_day * interval '1' day 
from abc_company;

自Postgres 9.4起,使用 make_interval() 函数

Since Postgres 9.4 this is easier done using the make_interval() function:

select create_ts + make_interval(days => num_of_day)
from abc_company;

这篇关于Postgres INTERVAL使用表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆