SQL - 如果条件在table2

时间:2016-07-20 23:07:54

标签: java sql tapestry

我不知道这是否可以在SQL中完成,但我在尝试时没有运气,所以我会感激一些帮助。

我有两张桌子:“房间”和“预订”。 “房间”显示酒店房间,“预订”是预订。例如:

从左到右,TABLE1,TABLE2和我想要的结果

enter image description here

例如,我想列出两个日期之间可用的房间,这种情况介于20和21之间。 房间1不可用(预订两个日期),房间2不可用(20个预订)所以它应列出如下内容:

有些帮助吗?我尝试了一些LEFT JOIN,blabla并没有得到正确的结果。对于日期NOT IN(date1,date2)对我不起作用,因为它不应该在date1和date2之间预订。

我非常感谢你的帮助。 感谢

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。这是outer join / null方法:

select t1.roomid, t1.door
from table1 t1 
    left join table2 t2 on t1.roomid = t2.roomid 
         and t2.date >= '2016-08-20' and t2.date <= '2016-08-21'
where t2.bookingid is null

关键是将where条件移至on的{​​{1}}部分。

这是使用join的另一个类似选项:

not exists