下表中的每一行表示status of a room on a specific date
我如何编写查询以过滤2016-07-09 - 2016-07-19内可用的过滤房间。
例如,对于room_id
23 ,2016-07-09和2016-07-之间的saleable
州应 true 19`。如果是这样,请选择结果。
当前SQL语句:
SELECT "room_skus"."id" AS t0_r0, "room_skus"."room_id" AS t0_r1, "room_skus"."price" AS t0_r2, "room_skus"."date" AS t0_r3, "room_skus"."created_at" AS t0_r4, "room_skus"."updated_at" AS t0_r5, "room_skus"."saleable" AS t0_r6, "room_skus"."annotation" AS t0_r7, "room_skus"."state" AS t0_r8, "rooms"."id" AS t1_r0, "rooms"."hotel_id" AS t1_r1, "rooms"."name" AS t1_r2, "rooms"."guests" AS t1_r3, "rooms"."created_at" AS t1_r4, "rooms"."updated_at" AS t1_r5, "rooms"."english_name" AS t1_r6, "hotels"."id" AS t2_r0, "hotels"."name" AS t2_r1, "hotels"."introduction" AS t2_r2, "hotels"."city_id" AS t2_r3, "hotels"."created_at" AS t2_r4, "hotels"."updated_at" AS t2_r5, "hotels"."checkin_time" AS t2_r6, "hotels"."checkout_time" AS t2_r7, "hotels"."region" AS t2_r8, "hotels"."english_name" AS t2_r9, "hotels"."address" AS t2_r10, "hotels"."official_website_link" AS t2_r11, "cities"."id" AS t3_r0, "cities"."country_id" AS t3_r1, "cities"."name" AS t3_r2, "cities"."created_at" AS t3_r3, "cities"."updated_at" AS t3_r4, "cities"."english_name" AS t3_r5, "cities"."code" AS t3_r6
FROM "room_skus" LEFT OUTER JOIN "rooms" ON "rooms"."id" = "room_skus"."room_id" LEFT OUTER JOIN "hotels" ON "hotels"."id" = "rooms"."hotel_id" LEFT OUTER JOIN "cities" ON "cities"."id" = "hotels"."city_id"
WHERE ("room_skus"."date" BETWEEN '2016-07-10 00:00:00.000000' AND '2016-07-30 23:59:59.999999') AND "cities"."id" = 1
关键是我不知道如何编写SQL stmt使其像WHERE (room_skus.date BETWEEN '2016-07-09 00:00:00.000000' AND '2016-07-19 23:59:59.999999')
和saleable
状态应该 true 在每个{{1}上对于2016-07-09至2016-07-19的持续日期。
room_sku.id
答案 0 :(得分:1)
由于您没有提供完整的信息,我将假设:
room
,每个房间有一排。room_skus
,每个房间和每天有一行(完整为相关时间范围设置)。saleable
为boolean NOT NULL
,date
定义为date NOT NULL
然后,您只需在所请求的时间段内选择所有没有saleable = FALSE
天的房间:
SELECT id
FROM room r
WHERE NOT EXISTS (
SELECT 1
FROM room_skus
WHERE date BETWEEN '2016-07-09' AND '2016-07-19'
AND room_id = r.id
AND NOT saleable
);