简体   繁体   English

将多个房间可用性查询合并为一个

[英]Combine multiple room availability queries into one

I'm currently trying to optimize an database by combining queries. 我目前正在尝试通过组合查询来优化数据库。 But I keep hitting dead ends while optimizing an room availability query. 但是,在优化房间可用性查询的同时,我一直在追求死胡同。

I have a room availability table where each records states the available number of rooms per date. 我有一个房间可用性表,其中每个记录都说明了每个日期的可用房间数。 It's formatted like so: 它的格式如下:

  • room_availability_id (PK) room_availability_id(PK)
  • room_availability_rid (fk_room_id) room_availability_rid(fk_room_id)
  • room_availability_date (2011-02-11) room_availability_date(2011-02-11)
  • room_availability_number (number of rooms available) room_availability_number(可用房间数)

The trouble is getting a list of rooms that are available for EACH of the provided days. 麻烦的是获得每个提供的日期可用的房间列表。 When I use IN() like so: 当我像这样使用IN()时:

WHERE room_availability_date IN('2011-02-13','2011-02-14','2011-02-15')
AND room_availability_number > 0

If the 14th has availability 0 it still gives me the other 2 dates. 如果14号有可用性0,它仍然给我其他2个日期。 But I only want that room_id when it is available on ALL three dates. 但是我只想在所有三个日期都可以使用room_id。

Please tell me there is a way to do this in MySQL other than querying each date/room/availability combination separately (that is what is done now :-( ) 请告诉我除了单独查询每个日期/房间/可用性组合之外,还有一种方法可以在MySQL中执行此操作(现在就是这样做:-()

I tried all sorts of combinations, tried to use room_availability_date = ALL (...), tried some dirty repeating subqueries but to no avail. 我尝试了各种组合,尝试使用room_availability_date = ALL(...),尝试了一些脏的重复子查询,但无济于事。

Thank you in advance for any thoughts! 提前感谢您的任何想法!

You would need to construct a query to group on the room ID and then check that there is availability on each date, which can be done using the having clause. 您需要构建一个查询以对房间ID进行分组,然后检查每个日期是否有可用性,这可以使用having子句来完成。 Leaving the where clause predicate in for room_availability_date will help to keep the query efficient (as indexes etc. can't be used with a having clause easily). 将where子句谓词保留为room_availability_date将有助于保持查询的有效性(因为索引等不能轻易地与having子句一起使用)。

SELECT 
    room_availability_rid

WHERE room_availability_date IN ('2011-02-13','2011-02-14','2011-02-15')
  AND room_availability_number > 0

GROUP BY room_availability_rid 

HAVING count(case room_availability_date when '2011-02-13' THEN 1 END) > 0
   AND count(case room_availability_date when '2011-02-14' THEN 1 END) > 0
   AND count(case room_availability_date when '2011-02-15' THEN 1 END) > 0

I think I can improve on a'r's answer: 我想我可以改进一个答案:

SELECT 
    room_availability_rid, count(*) n

WHERE room_availability_date IN ('2011-02-13','2011-02-14','2011-02-15')
  AND room_availability_number > 0

GROUP BY room_availability_rid 

HAVING n=3

Edit: This of course assumes that there is only one table entry per room per day. 编辑:当然这假设每个房间每天只有一个表项。 Is this a valid assumption? 这是一个有效的假设吗?

You can group by room ID, generate a list of dates available, and then see if all the dates you need are included. 您可以按房间ID分组,生成可用日期列表,然后查看是否包含您需要的所有日期。

This will give you a list of dates each room is available: 这将为您提供每个房间可用的日期列表:

select `room_availability_rid`,group_concat(`room_ availability_date`) as `datelist`
       from `table` where room_availability_number>0 
       group by `room_availability_rid`

Then we can add a having clause to get the rooms that are available on all of the dates we need: 然后我们可以添加一个having子句来获取我们需要的所有日期的房间:

select `room_availability_rid`,group_concat(`room_ availability_date`) as `datelist`
        from `table` where room_availability_number>0
        group by `room_availability_rid`
        having find_in_set('2011-02-13',`datelist`) and
               find_in_set('2011-02-14',`datelist`) and
               find_in_set('2011-02-15',`datelist`)

This should work. 这应该工作。 Test it for me will ya? 我会测试一下吗? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM