简体   繁体   English

SQL检查日期之间是否有空房间

[英]SQL To check if the room is available between the dates

I have a question in reference to a query to the database. 我有一个关于数据库查询的问题。 I have this table: 我有这张桌子:

在此处输入图片说明

In another window, i have a form with start and end date inputs and I want to do a sql to confirm if the room 23 is available between this dates, every day. 在另一个窗口中,我有一个带有开始日期和结束日期输入的表单,我想执行一条sql确认每天在此日期之间是否有23号房间可用。 I try with this sql: 我尝试使用此sql:

SELECT * FROM this_table 
WHERE room_type_id=23 
AND date="2018-03-06 00:00:00" 
AND date="2018-03-07 00:00:00" 
AND date="2018-03-08 00:00:00" 
AND date="2018-03-09 00:00:00"

This query doesn't work because is checking if any row has all the dates. 该查询不起作用,因为正在检查是否有所有日期的行。

My question is: How can i check if the room is available on this days? 我的问题是:我如何检查这几天房间是否可用?

This will answer your particular question with yes or no. 这将以是或否回答您的特定问题。 I'm not sure it's useful since the date search values are all specific in a hard-coded list. 我不确定它是否有用,因为日期搜索值在硬编码列表中都是特定的。

select coalesce(max('no'), 'yes') as available
from tbl t           
where room_type_id = 23 and
    dt in ("2018-03-06 00:00:00", "2018-03-07 00:00:00", 
           "2018-03-08 00:00:00", "2018-03-09 00:00:00");

If the sense of your logic is intended to find at least one open date then you might try something like this: 如果您的逻辑意图是至少找到一个开放日期,那么您可以尝试这样的事情:

select case when count(*) = 4 then 1 else 0 end as whatever_this_means
from tbl t
where room_type_id = 23 and
    dt in ("2018-03-06 00:00:00", "2018-03-07 00:00:00", 
           "2018-03-08 00:00:00", "2018-03-09 00:00:00");

The problem is going to be that you've hard-coded the value 4 now which is tied to the number of dates in the list later in the query. 问题在于您现在已经硬编码了值4,该值与查询中稍后列表中的日期数有关。 There are ways to handle this more dynamically but it's not entirely clear from your question the best way to do that. 有多种方法可以更动态地处理此问题,但您的问题尚不清楚该方法的最佳解决方案。

Perhaps using OR instead of AND between the dates? 也许在日期之间使用OR而不是AND?

SELECT * FROM this_table 
WHERE room_type_id=23 AND 
(date="2018-03-06 00:00:00" OR
date="2018-03-07 00:00:00" OR
date="2018-03-08 00:00:00" OR
date="2018-03-09 00:00:00")

shawnt00 gives me a good idea. shawnt00给了我一个好主意。 I modify a little this query changing the yes and no responses and it works for me. 我对该查询做了一些修改,更改了是和否响应,它对我有用。

select case when count(*) < 4 then 'no' else 'yes' end as available
from room_type_day t
where room_type_id = 23 and
    date in ("2018-03-07 00:00:00","2018-03-08 00:00:00","2018-03-09 00:00:00","2018-03-10 00:00:00");

if you store room info inside db when somebody reserve it the every record show that the room is not empty at it's date, now if you have another table that store room names you can select rooms that not exists any date for them 如果您在某人预订房间信息时将房间信息存储在db中,则每条记录都表明该房间在该日期不为空,现在,如果您有另一个存储房间名称的表,则可以选择不存在日期的房间

SELECT room_id FROM rooms WHEN room_id NOT IN (SELECT room_id FROM reserveTable WHERE date BETWEEN @StartDate AND @EndDate)

but if you store empty rooms in db(it may be a dirty database ;-) ) you can select rooms that are between your dates, 但是,如果您将空房间存储在db(它可能是一个肮脏的数据库;-))中,则可以选择日期之间的房间,

SELECT room_id FROM reserveTable WHEN date BETWEEN @StartDate AND @ EndDate

finally, it depends to your database structure, and your form definition. 最后,这取决于您的数据库结构和表单定义。
i hope this help you to find a good solution 我希望这可以帮助您找到一个好的解决方案

If 'this_table' store available room then you can return 0 or 1 with this trick : 如果'this_table'存储可用空间,则可以使用此技巧返回0或1:

Count the number of days available : 计算可用天数:

SELECT room_type_id, Count(*) as Av_days
FROM this_table
WHERE room_type_id=23 AND date BETWEEN @StartDate AND @ EndDate GROUP BY room_type_id

Then test the equality : 然后测试相等性:

SELECT (DATEDIFF(@EndDate, @StartDate) = Av_days )
 FROM
  (SELECT room_type_id, Count(*) as Av_days
  FROM this_table
   WHERE room_type_id=23 AND date BETWEEN @StartDate AND @ EndDate
  GROUP BY room_type_id) t

If you the same number of days, it will return 1 else 0 如果您的天数相同,则将返回1,否则返回0

You need to use OR instead of AND. 您需要使用OR而不是AND。 Your statement will only return a result if room 23 is equal to all 4 of those dates listed. 仅当房间23等于列出的所有四个日期时,您的对帐单才返回结果。

SELECT * FROM this_table 
WHERE room_type_id=23 
AND 
(
   date="2018-03-06 00:00:00" 
OR date="2018-03-07 00:00:00" 
OR date="2018-03-08 00:00:00" 
OR date="2018-03-09 00:00:00"
)

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

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