mysql 中两个日期之间的可用性
Availability between two dates in mysql
我想搜索两个日期之间的可用性。
这是我的tbl_bookingtable结构
slot_id from_date to_date hotel_id
1 2019-08-02 2019-08-06 2
2 2019-08-10 2019-08-14 2
3 2019-08-22 2019-08-24 2
4 2019-08-28 2019-08-29 2
下面是一些测试用例,这是系统应该如何工作的
1) I/P Date : 2019-08-08 to 2019-08-09 -> O/P - Available.
2) I/P Date : 2019-08-16 to 2019-08-18 -> O/P - Available.
3) I/P Date : 2019-08-11 to 2019-08-15 -> O/P - Not Available.
4) I/P Date : 2019-08-27 to 2019-08-28 -> O/P - Not Available.
这是我试过的查询
SELECT COUNT(*) as available from tbl_booking WHERE
(from_date BETWEEN '2019-08-08' AND '2019-08-09') AND
(to_date BETWEEN '2019-08-08' AND '2019-08-09')
我无法理解您的实际输出。从我的角度来看,我认为你需要这个
SELECT COUNT(*) as available from tbl_booking WHERE
('2019-08-08' BETWEEN from_date AND to_date) OR
('2019-08-09' BETWEEN from_date AND to_date)
我想搜索两个日期之间的可用性。
这是我的tbl_bookingtable结构
slot_id from_date to_date hotel_id
1 2019-08-02 2019-08-06 2
2 2019-08-10 2019-08-14 2
3 2019-08-22 2019-08-24 2
4 2019-08-28 2019-08-29 2
下面是一些测试用例,这是系统应该如何工作的
1) I/P Date : 2019-08-08 to 2019-08-09 -> O/P - Available.
2) I/P Date : 2019-08-16 to 2019-08-18 -> O/P - Available.
3) I/P Date : 2019-08-11 to 2019-08-15 -> O/P - Not Available.
4) I/P Date : 2019-08-27 to 2019-08-28 -> O/P - Not Available.
这是我试过的查询
SELECT COUNT(*) as available from tbl_booking WHERE
(from_date BETWEEN '2019-08-08' AND '2019-08-09') AND
(to_date BETWEEN '2019-08-08' AND '2019-08-09')
我无法理解您的实际输出。从我的角度来看,我认为你需要这个
SELECT COUNT(*) as available from tbl_booking WHERE
('2019-08-08' BETWEEN from_date AND to_date) OR
('2019-08-09' BETWEEN from_date AND to_date)