使用postgresql在rails中查询日期范围

时间:2014-12-31 11:46:21

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

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

  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.

1 个答案:

答案 0 :(得分:0)

如果您想确定某个范围是否包含其他范围:

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