我正在使用datetime,我想知道为什么我没有从我正在进行的查询中得到任何结果。
在rails c
:
>> Performance.find(6)
Performance Load (0.4ms) SELECT "performances".* FROM "performances" WHERE
"performances"."id" = $1 LIMIT 1 [["id", 6]]
我选择了第6个,因为我知道日期〜任意。以下是它的回应。
=> #<Performance id: 6, show: "", choreographer: "", location: "", tickets: "", created_at:
"2014-10-01 06:43:41", updated_at: "2014-10-01 06:43:41", has_passed: nil, event: nil, date:
"2014-12-06 07:15:00">
注意结束日期。它的格式为
2014-12-06 {...}
但是,当我尝试使用where查询
来提取此记录时Performance.where(date: (Time.now.midnight + 10.year)..(Time.now.midnight - 10.year))
返回
Performance Load (0.7ms) SELECT "performances".* FROM "performances"
WHERE
("performances"."date" BETWEEN '2024-10-01 04:00:00.000000' AND '2004-10-01 04:00:00.000000')
=> #<ActiveRecord::Relation []>
我并不感到惊讶,我做错了什么,但我不知道格式看起来如此相似。
我正在引用BETWEEN X and Y
以及Performance.find(6)格式化的格式。
如何更改查询以确保我获取第6条记录?
由于
答案 0 :(得分:2)
你必须在左边有一个最小的日期,因为BETWEEN运算符的行为如下:
expr BETWEEN begin_exp AND end_expr
// is the same as
expr >= begin_exp AND expr <= end_expr
没有日期超过&#34;大于&#34;比&#39; 2024-10-01 04:00:00.000000&#39;和&#34;小于&#34; &#39; 2004-10-01 04:00:00.000000&#39;。