如何获取两个日期之间的所有日期?

时间:2015-07-21 05:18:18

标签: mysql

请查看我的books表:

books

+-----+----------+------------+------------+
| id  | venue_id | from_date  | to_date    |
+-----+----------+------------+------------+
|  1  |  8       | 2015-07-21 | 2015-07-28 |
|  2  |  5       | 2015-08-03 | 2015-08-25 |
+-----+----------+------------+------------+

我想查看from_dateto_date之间的所有日期venue_id = 8

我的输出应该是:

Array
(
    [0] => Array
        (
             [id] => 1
             [venue_id] => 8
             [today] => 2015-07-21
        )
    [1] => Array
        (
             [id] => 1
             [venue_id] => 8
             [today] => 2015-07-22
        )
    [2] => Array
        (
             [id] => 1
             [venue_id] => 8
             [today] => 2015-07-23
        )
    [3] => Array
        (
             [id] => 1
             [venue_id] => 8
             [today] => 2015-07-24
        )
    [4] => Array
        (
             [id] => 1
             [venue_id] => 8
             [today] => 2015-07-25
        )
    [5] => Array
        (
             [id] => 1
             [venue_id] => 8
             [today] => 2015-07-26
        )
    [6] => Array
        (
             [id] => 1
             [venue_id] => 8
             [today] => 2015-07-27
        )
)

todayfrom_date日期到to_date日期计算。但我对这个问题的想法是空洞的。如何编写MySQL查询以获得类似上面输出的输出?

2 个答案:

答案 0 :(得分:2)

我认为以下查询应该获取结果。虽然我还没有测试过这个问题。

select b.id,b.venue_id,d.date from books b 
join
(select a.Date, "8" as venue_id2
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (1000 * c.a)) DAY as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between '2010-01-20' and '2010-01-24') d on d.venue_id2 = b.venue_id and b.venue_id = 8

答案 1 :(得分:0)

以下查询应该用于获得所需的结果。            select * from books where venue_id = 8 and from_date> ='2015-07-21'and to_date< ='2015-07-27'