【问题标题】:where query in daterange in rails with postgresql使用 postgresql 在 Rails 中的 daterange 中查询
【发布时间】:2014-12-31 11:46:21
【问题描述】:

如何在带有 Rails 4.2 的 postgresql 中使用带有 daterange 的 where 查询:

  ConsultingLocation Load (0.6ms)  SELECT  "consulting_locations".* FROM "consulting_locations" WHERE "consulting_locations"."deleted_at" IS NULL AND "consulting_locations"."id" = $1 LIMIT 1  [["id", 495]]
  ConsultingLocationDoctorSchedule Load (1.7ms)  SELECT "consulting_location_doctor_schedules".* FROM "consulting_location_doctor_schedules" INNER JOIN "consulting_location_doctors" ON "consulting_location_doctor_schedules"."consulting_location_doctor_id" = "consulting_location_doctors"."id" WHERE "consulting_location_doctors"."deleted_at" IS NULL AND "consulting_location_doctors"."consulting_location_id" = $1  [["consulting_location_id", 495]]
=> [#<ConsultingLocationDoctorSchedule:0x007fe7653f7538
  id: 1,
  consulting_location_doctor_id: 495,
  schedule_date: Mon, 05 Jan 2015 00:00:00 IST +05:30,
  slot_details:
   [{"end"=>"2015-01-05T03:00:00.000+00:00", "start"=>"2015-01-05T02:30:00.000+00:00", "title"=>" 2:30 am to  3:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T03:30:00.000+00:00", "start"=>"2015-01-05T03:00:00.000+00:00", "title"=>" 3:00 am to  3:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T04:00:00.000+00:00", "start"=>"2015-01-05T03:30:00.000+00:00", "title"=>" 3:30 am to  4:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T04:30:00.000+00:00", "start"=>"2015-01-05T04:00:00.000+00:00", "title"=>" 4:00 am to  4:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T05:00:00.000+00:00", "start"=>"2015-01-05T04:30:00.000+00:00", "title"=>" 4:30 am to  5:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T05:30:00.000+00:00", "start"=>"2015-01-05T05:00:00.000+00:00", "title"=>" 5:00 am to  5:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T06:00:00.000+00:00", "start"=>"2015-01-05T05:30:00.000+00:00", "title"=>" 5:30 am to  6:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T06:30:00.000+00:00", "start"=>"2015-01-05T06:00:00.000+00:00", "title"=>" 6:00 am to  6:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T07:00:00.000+00:00", "start"=>"2015-01-05T06:30:00.000+00:00", "title"=>" 6:30 am to  7:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T07:30:00.000+00:00", "start"=>"2015-01-05T07:00:00.000+00:00", "title"=>" 7:00 am to  7:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T08:00:00.000+00:00", "start"=>"2015-01-05T07:30:00.000+00:00", "title"=>" 7:30 am to  8:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T08:30:00.000+00:00", "start"=>"2015-01-05T08:00:00.000+00:00", "title"=>" 8:00 am to  8:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T09:00:00.000+00:00", "start"=>"2015-01-05T08:30:00.000+00:00", "title"=>" 8:30 am to  9:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T09:30:00.000+00:00", "start"=>"2015-01-05T09:00:00.000+00:00", "title"=>" 9:00 am to  9:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T10:00:00.000+00:00", "start"=>"2015-01-05T09:30:00.000+00:00", "title"=>" 9:30 am to 10:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T10:30:00.000+00:00", "start"=>"2015-01-05T10:00:00.000+00:00", "title"=>"10:00 am to 10:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T11:00:00.000+00:00", "start"=>"2015-01-05T10:30:00.000+00:00", "title"=>"10:30 am to 11:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T11:30:00.000+00:00", "start"=>"2015-01-05T11:00:00.000+00:00", "title"=>"11:00 am to 11:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T12:00:00.000+00:00", "start"=>"2015-01-05T11:30:00.000+00:00", "title"=>"11:30 am to 12:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T12:30:00.000+00:00", "start"=>"2015-01-05T12:00:00.000+00:00", "title"=>"12:00 pm to 12:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T13:00:00.000+00:00", "start"=>"2015-01-05T12:30:00.000+00:00", "title"=>"12:30 pm to  1:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T13:30:00.000+00:00", "start"=>"2015-01-05T13:00:00.000+00:00", "title"=>" 1:00 pm to  1:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T14:00:00.000+00:00", "start"=>"2015-01-05T13:30:00.000+00:00", "title"=>" 1:30 pm to  2:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T14:30:00.000+00:00", "start"=>"2015-01-05T14:00:00.000+00:00", "title"=>" 2:00 pm to  2:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T15:00:00.000+00:00", "start"=>"2015-01-05T14:30:00.000+00:00", "title"=>" 2:30 pm to  3:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T15:30:00.000+00:00", "start"=>"2015-01-05T15:00:00.000+00:00", "title"=>" 3:00 pm to  3:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T16:00:00.000+00:00", "start"=>"2015-01-05T15:30:00.000+00:00", "title"=>" 3:30 pm to  4:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T16:30:00.000+00:00", "start"=>"2015-01-05T16:00:00.000+00:00", "title"=>" 4:00 pm to  4:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T17:00:00.000+00:00", "start"=>"2015-01-05T16:30:00.000+00:00", "title"=>" 4:30 pm to  5:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T17:30:00.000+00:00", "start"=>"2015-01-05T17:00:00.000+00:00", "title"=>" 5:00 pm to  5:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T18:00:00.000+00:00", "start"=>"2015-01-05T17:30:00.000+00:00", "title"=>" 5:30 pm to  6:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T18:30:00.000+00:00", "start"=>"2015-01-05T18:00:00.000+00:00", "title"=>" 6:00 pm to  6:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T19:00:00.000+00:00", "start"=>"2015-01-05T18:30:00.000+00:00", "title"=>" 6:30 pm to  7:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T19:30:00.000+00:00", "start"=>"2015-01-05T19:00:00.000+00:00", "title"=>" 7:00 pm to  7:30 pm", "appointment_id"=>""}],
  start_and_end_time: Mon, 05 Jan 2015...Tue, 06 Jan 2015,
  deleted_at: nil,
  deleted_by_id: nil,
  created_at: Wed, 31 Dec 2014 16:21:59 IST +05:30,
  updated_at: Wed, 31 Dec 2014 16:21:59 IST +05:30>,
 #<ConsultingLocationDoctorSchedule:0x007fe7653f7380
  id: 2,
  consulting_location_doctor_id: 495,
  schedule_date: Tue, 06 Jan 2015 00:00:00 IST +05:30,
  slot_details:
   [{"end"=>"2015-01-06T09:30:00.000+00:00", "start"=>"2015-01-06T09:00:00.000+00:00", "title"=>" 9:00 am to  9:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T10:00:00.000+00:00", "start"=>"2015-01-06T09:30:00.000+00:00", "title"=>" 9:30 am to 10:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T10:30:00.000+00:00", "start"=>"2015-01-06T10:00:00.000+00:00", "title"=>"10:00 am to 10:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T11:00:00.000+00:00", "start"=>"2015-01-06T10:30:00.000+00:00", "title"=>"10:30 am to 11:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T11:30:00.000+00:00", "start"=>"2015-01-06T11:00:00.000+00:00", "title"=>"11:00 am to 11:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T12:00:00.000+00:00", "start"=>"2015-01-06T11:30:00.000+00:00", "title"=>"11:30 am to 12:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T12:30:00.000+00:00", "start"=>"2015-01-06T12:00:00.000+00:00", "title"=>"12:00 pm to 12:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T13:00:00.000+00:00", "start"=>"2015-01-06T12:30:00.000+00:00", "title"=>"12:30 pm to  1:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T13:30:00.000+00:00", "start"=>"2015-01-06T13:00:00.000+00:00", "title"=>" 1:00 pm to  1:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T14:00:00.000+00:00", "start"=>"2015-01-06T13:30:00.000+00:00", "title"=>" 1:30 pm to  2:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T14:30:00.000+00:00", "start"=>"2015-01-06T14:00:00.000+00:00", "title"=>" 2:00 pm to  2:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T15:00:00.000+00:00", "start"=>"2015-01-06T14:30:00.000+00:00", "title"=>" 2:30 pm to  3:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T15:30:00.000+00:00", "start"=>"2015-01-06T15:00:00.000+00:00", "title"=>" 3:00 pm to  3:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T16:00:00.000+00:00", "start"=>"2015-01-06T15:30:00.000+00:00", "title"=>" 3:30 pm to  4:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T16:30:00.000+00:00", "start"=>"2015-01-06T16:00:00.000+00:00", "title"=>" 4:00 pm to  4:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T17:00:00.000+00:00", "start"=>"2015-01-06T16:30:00.000+00:00", "title"=>" 4:30 pm to  5:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T17:30:00.000+00:00", "start"=>"2015-01-06T17:00:00.000+00:00", "title"=>" 5:00 pm to  5:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T18:00:00.000+00:00", "start"=>"2015-01-06T17:30:00.000+00:00", "title"=>" 5:30 pm to  6:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T18:30:00.000+00:00", "start"=>"2015-01-06T18:00:00.000+00:00", "title"=>" 6:00 pm to  6:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T19:00:00.000+00:00", "start"=>"2015-01-06T18:30:00.000+00:00", "title"=>" 6:30 pm to  7:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T19:30:00.000+00:00", "start"=>"2015-01-06T19:00:00.000+00:00", "title"=>" 7:00 pm to  7:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T20:00:00.000+00:00", "start"=>"2015-01-06T19:30:00.000+00:00", "title"=>" 7:30 pm to  8:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T20:30:00.000+00:00", "start"=>"2015-01-06T20:00:00.000+00:00", "title"=>" 8:00 pm to  8:30 pm", "appointment_id"=>""}],
  start_and_end_time: Tue, 06 Jan 2015...Wed, 07 Jan 2015,
  deleted_at: nil,
  deleted_by_id: nil,
  created_at: Wed, 31 Dec 2014 16:21:59 IST +05:30,
  updated_at: Wed, 31 Dec 2014 16:21:59 IST +05:30>,

通常我们会使用简单的日期 Table.where(start_and_end_time: Date.yesterday..Date.today)

我试过了:

where("start_and_end_time :@> ?",Date.yesterday..Date.today)

抛出这个错误:

ConsultingLocation.find(495).schedules.where("start_and_end_time :@> ?",Date.yesterday..Date.today)
  ConsultingLocation Load (0.5ms)  SELECT  "consulting_locations".* FROM "consulting_locations" WHERE "consulting_locations"."deleted_at" IS NULL AND "consulting_locations"."id" = $1 LIMIT 1  [["id", 495]]
PG::SyntaxError: ERROR:  syntax error at or near ":"
LINE 1: ...sulting_location_id" = $1 AND (start_and_end_time :@> '2014-...

它没有工作。一般如何使用日期范围


这也不起作用:

ConsultingLocation.find(495).schedules.where(start_and_end_time: Date.yesterday..Date.today)
  ConsultingLocation Load (103.0ms)  SELECT  "consulting_locations".* FROM "consulting_locations" WHERE "consulting_locations"."deleted_at" IS NULL AND "consulting_locations"."id" = $1 LIMIT 1  [["id", 495]]
PG::InvalidTextRepresentation: ERROR:  malformed range literal: "2015-01-01"
LINE 1: ...on_doctor_schedules"."start_and_end_time" BETWEEN '2015-01-0...
                                                             ^
DETAIL:  Missing left parenthesis or bracket.

【问题讨论】:

  • 您已经通过展示它的工作原理回答了您的问题,为什么还要尝试另一种不起作用的方式?
  • @MaurícioLinhares 现在检查编辑。
  • 好吧,它不起作用,在ActiveRecord中自动查询日期的方法是Table.where(start_and_end_time: Date.yesterday..Date.today)。不知道你为什么要尝试一些显然在这里不起作用的东西。
  • @MaurícioLinhares 这不起作用,我已经尝试过了!我没有尝试明显不起作用的东西,因为文档指向与我发布的内容类似的内容。我更新了这个问题,因为其他人可能会这么认为。
  • 您在 Rails 文档中的哪些地方找到了您尝试做的任何参考资料?

标签: ruby-on-rails postgresql date-range


【解决方案1】:

如果要判断一个范围是否包含另一个范围:

where("start_and_end_time && daterange(?, ?, '[]')", Date.yesterday, Date.today)

如果您想知道某个日期是否在某个范围内:

where("start_and_end_time @> ?::date", Date.yesterday)

我发现这些文章很有帮助:

  1. https://wiki.postgresql.org/images/7/73/Range-types-pgopen-2012.pdf
  2. https://wiki.postgresql.org/wiki/RangeTypes
  3. http://edgeguides.rubyonrails.org/active_record_postgresql.html#range-types
  4. https://www.postgresql.org/docs/9.3/static/rangetypes.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2018-05-29
    • 2013-01-27
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多