查找日期范围内的所有内容

问题描述:

我有一个 MySQL 的住宿表,其中每个住宿都用 from_dateto_date 列定义.我想找到用户定义的范围内的所有停留.例如,假设从 01.01.201501.03.2015 有一段停留,我希望此条目包含在 01.01.2015 的报告中> 到 31.01.2015 以及从 01.02.201528.02.2015 和在 01.03.2015 到 31.03.2015.我已将所有内容存储为时间戳(以秒为单位).有人可以举例说明如何实现这一点吗?

I've got a MySQL table of stays, where each stay is defined with a from_date and to_date column. And I want to find all stays in range defined by user. For example, given that there is a stay from 01.01.2015 to 01.03.2015 I want this entry to be included in reports from 01.01.2015 to 31.01.2015 but also from 01.02.2015 to 28.02.2015 and in 01.03.2015 to 31.03.2015. I've got everything stored as timestamps (in seconds). Could somebody, please, give an example how to achieve this?

好的,感谢其他答案,我得到了最好的结果(stay_range_start=selected_range_start) 在 mysql 中:

Ok, thanks to the other answers I got the best results with (stay_range_start<=selected_range_end) && (stay_range_end>=selected_range_start) in mysql:

SELECT * FROM stays
WHERE  `t0`.`from_date` <= $to_date 
AND `t0`.`to_date` >= $from_date