我想在sql中的两个日期之间进行搜索

时间:2016-12-30 07:49:59

标签: php mysql

我有两张桌子,这个桌子名称是“room”Table Rooms,另一张是“预订”!Table Bookings

我现在加入了两张桌子,当我在

之间搜索时,我想要值

book_form = "2016-12-30"book_to = "2016-12-31"

它将返回true,因为这两个日期在“预订”表中不存在,并且在搜索之间

book_form = "2016-12-30"book_to = "2017-01-05"book_form = "2017-01-03"book_to = "2017-01-15"

它将返回false,因为此日期存在于预订表中。

这是我的疑问。

select * from rooms join room_book on rooms.room_id = room_book.room_id 
where status = 'available' and room_book.book_from NOT BETWEEN '2016-12-30' 
AND room_book.book_to NOT BETWEEN '2016-12-31'

2 个答案:

答案 0 :(得分:1)

Kishan Patel的答案将返回两个日期之间的所有记录。因此,如果你想要一个真或假,如果两个日期之间有空房,你可以这样做:

SELECT IF(COUNT(*) > 0, TRUE, FALSE) AS NewResult
from rooms 
join room_book on rooms.room_id = room_book.room_id
where status = 'available' 
and room_book.book_from >= '2016-12-30' AND room_book.book_to <= '2016-12-31'

答案 1 :(得分:0)

尝试这一次返回两个日期之间的所有记录

select * from rooms 
join room_book on rooms.room_id = room_book.room_id
where status = 'available' 
and room_book.book_from >= '2016-12-30' AND room_book.book_to <= '2016-12-31'

确保它对您有用