我在 Postgres 10.6 数据库中有一个表,其中有一列 tstzrange
类型。
我需要插入/更新具有定义的下限但当前时间值作为范围上限的行,因此 NOW()
作为上限值。
尝试过以下方法:
UPDATE table_name
SET date_range = ['2018-03-23 00:00:00-05', now())
WHERE id = 3;
有没有办法使用内置函数或子查询?
最佳答案
使用范围构造函数。 The manual:
Each range type has a constructor function with the same name as the range type. Using the constructor function is frequently more convenient than writing a range literal constant, since it avoids the need for extra quoting of the bound values. The constructor function accepts two or three arguments. The two-argument form constructs a range in standard form (lower bound inclusive, upper bound exclusive), while the three-argument form constructs a range with bounds of the form specified by the third argument. The third argument must be one of the strings “
()
”, “(]
”, “[)
”, or “[]
”.
所以:
UPDATE table_name
SET date_range = tstzrange('2018-03-23 00:00:00-05', now())
WHERE id = 3;
我假设您知道 now()
解析为事务的开始时间。
关于sql - 如何使用 NOW() 函数作为范围的上限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56690533/