我正在做酒店预定系统一个表中有开始日期和截止日期等比如一个房间 从 1月3号12点 到 1月4号18点已经被预约了接下来我查询 从 1月3号07点 到 1月4号20点有没有房间满足条件,该怎么写呢?最好不要增加column

解决方案 »

  1.   

    select 房间 from 房间表 a where not exists
    (
    select 1 from 预约表 where
    房间=a.房间 and
    (
    (开始日期<='1月3号07点' and '1月3号07点'<截止日期)
    or (开始日期<'1月4号20点' and '1月4号20点'<=截止日期)
    or ('1月3号07点'<=开始日期 and 开始日期<'1月4号20点')
    or ('1月3号07点'<截止日期 and 截止日期<='1月4号20点')
    )
    )