简体   繁体   English

MySQL查询比较2个日期范围

[英]mysql query comparing 2 date ranges

here's my data structure: 这是我的数据结构:

seasons
id  from         to           name      
----------------------------------------
1   2015-11-01   2015-12-15   season1   
2   2015-12-16   2015-12-30   season2   
3   2015-12-31   2016-01-20   season3   

rooms_free
id  from         to           room_id
----------------------------------------
1   2015-11-26   2015-11-30   1   
2   2015-12-19   2015-12-28   2
3   2015-12-22   2015-12-29   3

i need an sql query which will join both tables by date range returning the following result: 我需要一个SQL查询,该查询将按日期范围连接两个表,并返回以下结果:

id_room  room_from    room_to      season_id    season_name
-------------------------------------------------------------
1        2015-11-26   2015-11-30   1            season1   
2        2015-12-19   2015-12-28   2            season2
3        2015-12-22   2015-12-29   2            season2

could this be done using normal statements or would i need a mysql function? 可以使用普通语句完成此操作,还是需要mysql函数? any ideas? 有任何想法吗?

ps: the really tricky part is when there's several seasons per room .. ps:真正棘手的部分是每个房间有几个季节时..

This is the best explanation about overlaping data ranges 这是有关重叠数据范围的最佳解释

Determine Whether Two Date Ranges Overlap 确定两个日期范围是否重叠

SQL Fiddle Demo SQL小提琴演示

SELECT rf.room_id as id_room ,
       rf.from as room_from,
       rf.to as room_to,
       s.id as season_id,
       s.name as season_name
FROM rooms_free rf
JOIN seasons s
  ON (rf.From <= s.to)  and  (rf.to >= s.from)

OUTPUT OUTPUT

| room_id |                       from |                         to | id |    name |
|---------|----------------------------|----------------------------|----|---------|
|       1 | November, 26 2015 00:00:00 | November, 30 2015 00:00:00 |  1 | season1 |
|       2 | December, 19 2015 00:00:00 | December, 28 2015 00:00:00 |  2 | season2 |
|       3 | December, 22 2015 00:00:00 | December, 29 2015 00:00:00 |  2 | season2 |

SELECT rooms_free.id as id_room, rooms_free.from as room_from , rooms_free.to as room_to, seasons.id as season_id, seasons.name as season_name FROM rooms_free join seasons on rooms_free. 选择Rooms_free.id作为id_room,并将rooms_free.from作为room_from,rooms_free.to作为room_to,seasons.id作为season_id,seasons.name作为season_name FROM rooms_free在rooms_free中加入季节。 from >=seasons.from and rooms_free. from > = seasons.from和rooms_free。 to <=seasons.to to <= seasons.to

select rooms_free.id as id_room, rooms_free.from as room_from, rooms_free.to as room_to, seasons.id as season_id,  seasons.name as season_name
from rooms_free
join seasons
on ((rooms.from <= seasons.from) and (seasons.from <= rooms.to)) or ((seasons.from <= rooms.from) and (rooms.from <= seasons.to))

The statement above checks for strict overlapping. 上面的语句检查是否存在严格的重叠。 If there are several seasons per room, then you can use group by , having and group_concat . 如果有每间客房的几个赛季,那么你可以使用group byhavinggroup_concat

Select rooms_free.id, rooms_free.from, rooms_free.to, seasons.id,       seasons.name 
from rooms_free , seasons 
where (rooms_free.from >= season.from and rooms_free.to <= seasons.to)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM