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

人盡茶涼 提交于 2021-01-29 05:31:38

问题


I'm trying to make a query based on a field ``time` to get the last week data:

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

but I can't make it work.

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')

Thing is I get this condition from official docs of timescaleDB, supposed to be postgres compatible:

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

Why is it happening, and what should I do ?


回答1:


I think you need to use whereRaw because you have function calls in your expression.

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



回答2:


Try hour without quotes

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


来源:https://stackoverflow.com/questions/63057183/invalid-input-syntax-for-type-timestamp-now-interval-12-hours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!