我使用postgresql作为我的数据库。
目前我有时序模型,其中包含SELECT Debit,
Credit,
SUM((isnull(Credit,0) - isnull(Debit,0))) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as Balance
FROM @tab
和open_at
字段。两个字段的数据类型都是时间。
close_at
以上时间记录的开放时间是凌晨4点,关闭时间是凌晨1点。当前时间是6:46。所以当我在下面查询时
2.4.2 :077 > Time.current
=> Tue, 20 Mar 2018 06:46:28 UTC +00:00
=> #<Timing id: 3, day: "Tuesday", open_at: "2000-01-01 04:00:00", close_at: "2000-01-01 01:00:00", warehouse_id: 9, created_at: "2018-03-19 10:45:36", updated_at: "2018-03-20 06:34:16">
它应该是结果,但结果是空的但是当我改变关闭时间到晚上11点意味着23:00:00。并运行以下查询
2.4.2 :079 > Timing.where("open_at < ? and close_at > ?", Time.current, Time.current)
Timing Load (0.7ms) SELECT "timings".* FROM "timings" WHERE (open_at < '2018-03-20 06:48:09.004429' and close_at > '2018-03-20 06:48:09.004496') LIMIT $1 [["LIMIT", 11]]
=> #<ActiveRecord::Relation []>
结果来了。任何人都可以解释时间问题吗?或者如何查询我是否想要获得正确的记录
**
更新 - 在schema.rb中添加时间表
**
schema.rb
Timing.where("open_at < ? and close_at > ?", Time.current, Time.current)
Timing Load (0.8ms) SELECT "timings".* FROM "timings" WHERE (open_at < '2018-03-20 06:49:12.307156' and close_at > '2018-03-20 06:49:12.307205') LIMIT $1 [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Timing id: 3, day: "Tuesday", open_at: "2000-01-01 04:00:00", close_at: "2000-01-01 23:59:00", warehouse_id: 9, created_at: "2018-03-19 10:45:36", updated_at: "2018-03-20 06:49:07">]>
答案 0 :(得分:0)
如果您的问题是时间格式,您可以使用下一个:
Time.now.strftime("%I:%M %p") #=> It returns current time in AM/PM format
Time.strptime( '1:00 PM', '%I:%M %p') #=> return time in 24 hours format
=> 2018-03-20 13:00:00 +0300
并用它向DB发出请求。