[英]Rails sql query difference between two dates more then 2 weeks
I have a table that has 2 columns "date_from" and "date_to". 我有一个具有2列“ date_from”和“ date_to”的表。 I have to make a query which should return results where "date_to" - "date_from" == "2 weeks".
我必须进行查询,该查询应返回“ date_to”-“ date_from” ==“ 2周”的结果。
Something like this: 像这样:
User.where('date_to - date_from == 2.weeks').
Define this on user: 在用户上定义:
def self.two_weeks
where(date_to: date_from + 2.weeks)
end
Then you can just call User.two_weeks
然后,您可以致电
User.two_weeks
Can also be done as a scope, if you prefer: 如果您愿意,也可以作为范围来完成:
scope :two_weeks, -> { where(date_to: date_from + 2.weeks) }
It's good to keep these kinds of things on the model anyway, so that you can change them in one place if they need to be tweaked 最好还是将这些内容保留在模型中,以便在需要调整时可以将它们更改为一个位置。
这应该工作,至少对于mysql
User.where('1 + DATEDIFF(date_to, date_from) >= 14')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.