简体   繁体   English

查看日期范围的房间可用性

[英]check room availability with date range

Hello i have 2 tables rooms and bookings but my table sturcture little bit different, in bookings table there are roomid,date,status for example: 您好我有2个房间和预订,但我的餐桌结构有点不同,在预订表中有roomid,日期,状态例如:

101,2012-12-10,0
101,2012-12-11,0
101,2012-12-12,1
101,2012-12-13,0
102,2012-12-10,0
102,2012-12-11,0
102,2012-12-12,0

and i would like to find available rooms between 2012-12-10 and 2012-12-13 according this request only room 102 should be return. 我想在2012-12-10和2012-12-13之间找到可用的房间,根据这个要求,只有102房间应该返回。

i've tried 我试过了

SELECT id 
FROM status 
WHERE status='0' 
  AND date between '2012-12-10' AND '2012-12-13' 
GROUP BY id

it doesn't work because even find only one available row it returns true for 101 它不起作用,因为即使只找到一个可用行,它返回101为真

so 101 is available for 2012-12-11 then showing like available but not okay for our data range. 所以101可用于2012-12-11然后显示可用但不适合我们的数据范围。

If the date has a time associated with it you need to do something like this: 如果日期有与之相关的时间,您需要执行以下操作:

SELECT id 
FROM status 
WHERE status='0' 
  AND date between '2012-12-10 00:00:00' AND '2012-12-13 23:59:59' 
GROUP BY id

You can also use Date(date) instead of including times, but this might be less efficient. 您也可以使用日期(日期)而不是包括时间,但这可能效率较低。

You should be able to use the following: 您应该能够使用以下内容:

SELECT roomid 
FROM status s1
WHERE status='0' 
  AND date between '2012-12-10' AND '2012-12-13' 
  and not exists (select roomid
                  from status  s2
                  where status='1' 
                    AND date between '2012-12-10' AND '2012-12-13'
                    and s1.roomid = s2.roomid)
GROUP BY roomid

See SQL Fiddle with demo 请参阅SQL Fiddle with demo

Try this: 试试这个:

SELECT rd.* 
FROM room_details rd 
INNER JOIN (SELECT DISTINCT roomid FROM bookings 
            WHERE roomid NOT IN (SELECT roomid FROM bookings 
                                WHERE STATUS =0 AND DATE BETWEEN '2012-12-10' AND '2012-12-13')
                ) AS a ON rd.roomid = a.roomid

This will select all rooms that have no row with status=1 in the interval you select. 这将选择在您选择的时间间隔内没有status=1行的所有房间。 Notice that I am not using between : if status=1 on 13th of December, the room is still available in the interval, that's why I'm using >= and < : 请注意,我between没有使用:如果12月13日status=1 ,则房间仍在间隔期间可用,这就是为什么我使用>= and <

SELECT roomid
FROM status
WHERE `date` >= '2012-12-10'
      AND `date` < '2012-12-13'
GROUP BY roomid
HAVING sum(status.status=1)=0

If there could also be some missing days in your table, and if that means that the room is not booked but also not available, you could also use this query: 如果您的桌子中可能还有一些遗失的日子,如果这意味着房间没有预订但也没有,您也可以使用此查询:

SELECT roomid
FROM status
WHERE `date` >= '2012-12-10'
      AND `date` < '2012-12-13'
      AND status=0
GROUP BY roomid
HAVING count(*)=DATEDIFF('2012-12-13', '2012-12-10')

that checks that the number of days in the interval is equal to the number of rows with status=0. 检查间隔中的天数是否等于status = 0的行数。

The SQL BETWEEN condition will return the records where expression is within the range of value1 and value2 (inclusive). SQL BETWEEN条件将返回表达式在value1和value2(包括)范围内的记录。 So row 101,2012-12-10,0 should be returned for above mentioned query. 因此,对于上述查询,应返回行101,2012-12-10,0

Try this: 试试这个:

SELECT roomid
FROM status
WHERE 'date_from' <= "2012-12-10"  AND 'date_to' > "2012-12-10" AND 'status'= 0;

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

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