sql - 如何使用 NOW() 函数作为范围的上限?

标签 sql postgresql insert range range-types

我在 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/

相关文章:

c - C 中的哈希表 : Does testing functions using manual arguments differ from user inputted arguments

带有自动增量的 MySQL 插入 - 是否需要插入忽略以避免并发问题?

c# - 如何使用 C# 在 SQL Server 2008 中插入多个列表?

sql - Postgres 字符串数组的唯一约束/索引

java - Spring jdbc 模板中的 BadSqlGrammarException

postgresql - 使用外列迭代更新列

mysql - 如何更改对 phpmyadmin SQL/SQL 服务器的语法 SQL 访问

c# - 向具有标识主键的 sql 数据库添加一行

mysql - SQL Server 通过内部连接三个或更多表来选择不同的最新值

sql - 获取行中日期值之前的最新日期