从包含日期范围的表生成每月XML

时间:2013-07-10 06:37:20

标签: mysql database date-range

我有一张桌子 -

test_table(booking_id, booking_description, start_date, end_date)

示例数据 -

1 | Some booking | 06/30/2013 | 08/01/2013
2 | Some new one | 08/05/2013 | 09/01/2013
3 | Some new two | 09/03/2013 | 09/05/2013

现在我想使用一些java代码生成每月xml文件(没问题,我会写),我会将月份和年份(基本上是月份的开始和结束日期)传递给mysql查询和我想要一些表格 -

月= 7,2013年

1 | Some booking | 07/01/2013
1 | Some booking | 07/02/2013
...

月= 9,年= 2013

2 | Some new one | 09/01/2013
  |              | 09/02/2013
3 | Some new two | 09/03/2013
...

我正在寻找从开始日期到结束日期使用java循环并查询mysql以查明此日期是否在日期范围内,如果它来了我会添加其他细节我会添加空白。但这将是一个可怕的方法(将进行30次mysql查找),我认为这是最后的选择。

是否有其他方法可以使用一个或两个mysql查询并以格式获取数据。

编辑:

month = 7, year = 2013

Select * 
from booking_details 
where  month(start_date) <= 7 and year(start_date) <= 2013 and 
month(end_date) >= 7 and year(end_date) >= 2013

我开发了这个查询,但仍然不确定它是否适用于所有可能的场景。

1 个答案:

答案 0 :(得分:0)

基于我对这个问题的理解,你想要这样的东西:

declare @date datetime
Select booking_id, booking_description, start_date --you don't indicate which date field you want in the results
from test_table
where (start_date between @date and date_add(@date, INTERVAL 1 MONTH))
or (end_date between @date and date_add(@date, INTERVAL 1 MONTH))

SQL可能不准确,我知道TSQL不是MySQL,但这应该很接近。

相关问题