mysql 判断日期是否在某范围内,SQL检查一组日期是否在指定的日期范围内

Hi guys I have a table which holds dates when a room is unavailable in the schema:

ROOM_ID | DATE_UNAVAILABLE

I need a sql query that checks if a room is available during between a range of two dates - some thing along the line of

Select All rooms that are constantly available between date 1 and date 2

or rather

select all rooms that don not have a date entered in the date unavailable table which falls between date 1 and date 2

I'm using php MySQL here

Thanks

解决方案

The inner query finds the room that are not available, then we use a Not-exists left join to remove those dates from our results, leaving us with available rooms only.

SELECT r.ROOM_ID

FROM rooms r LEFT JOIN (

SELECT ROOM_ID

FROM tableName

WHERE DATE_UNAVAILABLE BETWEEN 'Date1' AND 'Date2'

GROUP BY ROOM_ID

) g ON r.ROOM_ID = g.ROOM_ID

WHERE g.ROOM_ID IS NULL

Alternativly, if you have correct indexes in place, skipping the group may be faster:

SELECT r.ROOM_ID

FROM rooms r LEFT JOIN tableName g ON r.ROOM_ID = g.ROOM_ID

AND g.DATE_UNAVAILABLE BETWEEN 'Date1' AND 'Date2'

WHERE g.ROOM_ID IS NULL

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值