时间戳类型的输入语法无效:"NOW() - INTERVAL '12 hours'"

Invalid input syntax for type timestamp: "NOW() - INTERVAL '12 hours'"

我正在尝试基于字段“时间”进行查询以获取上周数据:

Measure::where('time', '>', "NOW() - INTERVAL '12 hours'")->get();

但我做不到。

I get this message: 

SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type timestamp: "NOW() - INTERVAL '12 hours'" (SQL: select * from "measures" where "time" > NOW() - INTERVAL '12 hours')

事情是我从 timescaleDB 的 official docs 得到这个条件,应该是 postgres 兼容的:

SELECT COUNT(*) FROM conditions WHERE time > NOW() - INTERVAL '12 hours';

为什么会这样,我该怎么办?

我认为您需要使用 whereRaw,因为您的表达式中有函数调用。

Measure::whereRaw("time > NOW() - INTERVAL '12 hours'")->get();

试试不带引号的 hour

SELECT COUNT(*) FROM conditions WHERE time > NOW() - INTERVAL 12 hour;