如何让我的查询工作?
我想从我的SQL中获取未列出今天日期的数据。
这是我的问题:
String sql = "select Room, Name, Age, EmailAddress, Phone, Nationality, CheckIn, CheckOut from customer where CURDATE() NOT IN (SELECT Room FROM customer WHERE CURDATE() between CheckIn and Checkout)";
答案 0 :(得分:1)
这是您的查询:
select c.*
from customer c
where CURDATE() NOT IN (SELECT c2.Room
FROM customer c2
WHERE CURDATE() between c2.CheckIn and c2.Checkout
);
在不知道您的数据结构或您想要查询的确切内容的情况下,很难说出查询应该是什么。
然而,这将是一个非常非常不寻常的数据模型,其中名为Room
的列与curdate()
相当。我认为这至少是你问题的一部分。
如果您想获得今天未预订的房间,请从rooms
表开始(我假设您有一个!):
select r.*
from rooms r
where not exists (select 1
from bookings b -- a more natural name for a table with bookings
where curdate() between b.CheckIn and b.CheckOut
);
答案 1 :(得分:0)
为什么不使用NOT BETWEEN
:
SELECT Room, Name, Age, EmailAddress, Phone, Nationality, CheckIn, CheckOut
FROM customer
WHERE CURDATE() NOT BETWEEN CheckIn AND Checkout