简体   繁体   English

滑轨| SQL的日期时间和日期

[英]Rails | SQL where for datetime and date

I am trying to fetch records on database on localhost. 我正在尝试获取本地主机上数据库的记录。

My columns from & to are dateranges and I would like to convert daterange to date to find records. 我从&到的列是日期范围,我想将日期范围转换为日期以查找记录。 date = Date.parse(2017-02-17) sc = schedules.where("from <= ? AND to >= ?",date,date) gives error; date = Date.parse(2017-02-17) sc = schedules.where("from <= ? AND to >= ?",date,date)给出错误;

SELECT "schedule_days".* FROM "schedule_days" WHERE "schedule_days"."car_id" = ? AND (from <= '2017-02-17' AND to >= '2017-02-17') [["car_id", 2]] SQLite3::SQLException: near "from": syntax error: SELECT "schedule_days".* FROM "schedule_days" WHERE "schedule_days"."car_id" = ? AND (from <= '2017-02-17' AND to >= '2017-02-17') ActiveRecord::StatementInvalid: SQLite3::SQLException: near "from": syntax error: SELECT "schedule_days".* FROM "schedule_days" WHERE "schedule_days"."car_id" = ? AND (from <= '2017-02-17' AND to >= '2017-02-17')

Is this problem because from & to are datetime?. 这是因为从&到日期时间是问题吗? I also tried; 我也尝试过;

schedules.where("date(from) <= ? AND date(to) >= ?",date,date) no luck. schedules.where("date(from) <= ? AND date(to) >= ?",date,date)没有运气。 I will then push this code to heroku postgresql 然后,我将这段代码推送到heroku postgresql

FROM is a reserved word in SQL. FROM是SQL中的保留字。 It could be that the parser does not realize that you are referring to a column in a table and not using the FROM keyword. 可能是因为解析器没有意识到您是在引用表中的列并且没有使用FROM关键字。

Did you already try using a different variable name, or an alias? 您是否已经尝试使用其他变量名或别名?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM