sql - 时间戳间隔的 Postgres 语法错误

标签 sql postgresql timestamp

Postgres 在这里;我有一个包含以下字段的 Users 表:

create table Users ( 
    id bigserial primary key, 
    isAdmin boolean not null default false, 
    beginDate timestamp with time zone not null, 
    updated timestamp with time zone
);

我想编写一个查询来获取任何 Users 记录:

  • beginDate 值在过去 24 小时内(含);
  • 有一个 更新的(绝对)超过 24 小时

到目前为止我最好的尝试是:

select *
from
Users
where
beginDate >= NOW() - INTERVAL 1 DAY and
updated < NOW() - INTERVAL 1 DAY

但这给他们带来了以下错误:

ERROR: syntax error at or near "1"
  Position: 59

beginDate >= NOW() - INTERVAL 1 DAY and
                              ^
1 statement failed.

Execution time: 0.03s

关于修复的任何想法?

最佳答案

正确的语法应该是这样的:

beginDate >= NOW() - INTERVAL '1 DAY' and
updated < NOW() - INTERVAL '1 DAY'

您可以在此处找到更多信息:https://www.postgresql.org/docs/current/functions-datetime.html

关于sql - 时间戳间隔的 Postgres 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55620281/

相关文章:

javascript - 序列化嵌套模型的 where 条件

c# - Stopwatch.Gettimestamp 是否会翻转?还是回滚?

MySQL 查询到 MS Access 查询

sql - 需要使用表名和表架构列出 SQL Server 数据库中的所有触发器

php - Mysql DELETE 查询在 PMA 中有效,但在我的 PHP 脚本中无效

python - 使用 psycopg2 将类型转换应用于参数中的数组项

php - 无法将数据插入第二个数据库连接

arrays - 在 R 中将多列转换为 Json 数组?

database - IMMUTABLE、STABLE 和 VOLATILE 关键字如何影响函数的行为?

sql - 此时如何选择 4 天前的日期?