数据库中有记录如下:
房间号 开始日期 结束日期 天数
1 2010-1-19          2010-1-21          3
2 2010-1-19    2010-1-23          5目的:
取出开始日期在20号到22号的记录,并转成行,即将上述两条转成下面的条:
房间号 日期
1 2010-1-20
1 2010-1-21
2 2010-1-20
2 2010-1-21
2 2010-1-22
谢谢解答,100分送出。

解决方案 »

  1.   

    简单,生成一个日历表,字段日期,内容从2010-1-1 - 2010-12-31
    select a1.* from 日历表 a1
    inner join ( 
    select max(日期) as ma,min(日期) as mi from (
    select 开始日期 as 日期 from tt
    union
    select 结束日期 from tt) a) t
    on a1.日期 between mi and ma 
      

  2.   

    select a1.* from 日历表 a1 
    inner join ( 
    select max(日期) as ma,min(日期) as mi from ( 
    select 开始日期 as 日期 from tt 
    union 
    select 结束日期 from tt) a) t 
    ON a.日期 BETWEEN mi+ INTERVAL 1 DAY  AND ma - INTERVAL 1 DAY
      

  3.   

    订房系统啊 创建一张日历表 calendar (cdate date primary key,wkType int) wkType : 工作日:0,周未:1,国假:2
    cdate wktype
    2010-01-01 2
    2010-01-02 1
    2010-01-03 1
    2010-01-04 0
    2010-01-05 0
    2010-01-06 0
    2010-01-07 0
    2010-01-08 0
    2010-01-09 1
    2010-01-10 1
    2010-01-11 0
    2010-01-12 0
    ...
    然后SQL语句如下
    select a.房间号,c.cdate
    from 数据库中有记录如下 a inner join calendar c on (a.开始日期<=c.cdate and a.结束日期>=c.cdate)
    where 开始日期<='2010-01-20' and 结束日期>='2010-01-22'
      

  4.   

    select a.房间号,c.cdate
    from 数据库中有记录如下 a inner join calendar c on (a.开始日期<=c.cdate and a.结束日期>=c.cdate)
    where a.开始日期<='2010-01-20' and a.结束日期>='2010-01-22'
    and c.cdate between '2010-01-20' and '2010-01-22'