如何使用NOW()函数作为范围的上限? [英] How to use the NOW() function as upper bound of a range?
问题描述
我在Postgres 10.6数据库中有一个表,该表的列类型为 tstzrange
。
我需要插入/更新具有定义的下限但行的当前时间为范围上限的行,因此 NOW()
为上限值。
I have a table with a column of type tstzrange
in a Postgres 10.6 database.
I need to insert / update rows with a defined lower bound but the value of the current time for the upper bound of the range, so NOW()
as upper value.
尝试过以下操作:
UPDATE table_name
SET date_range = ['2018-03-23 00:00:00-05', now())
WHERE id = 3;
是否可以使用内置函数或子查询?
Is there a way to use a built-in function or a subquery maybe?
推荐答案
使用 范围构造函数 。 手册:
每个范围类型都有一个构造函数,其名称与
范围类型相同。与使用范围文字常量相比,使用构造函数通常更方便
,因为它避免了
需要额外引用绑定值的情况。构造函数
接受两个或三个参数。两参数形式构造一个标准形式的
范围(下限包含,上限排斥),
,而三参数形式构造一个范围为
形式的范围,该范围由第三个论点。第三个参数必须是字符串()
,(]
,[)
或[]
。
所以:
UPDATE table_name
SET date_range = tstzrange('2018-03-23 00:00:00-05', now())
WHERE id = 3;
我假设您知道 now()
解析为交易的开始时间。
I assume you are aware that now()
resolves to the start time of the transaction.
- Difference between now() and current_timestamp
这篇关于如何使用NOW()函数作为范围的上限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!