Mysql从日期范围列表生成每个日期

时间:2016-04-14 12:54:41

标签: mysql date

我有一个查询(从bla .. bla中选择*)产生日期范围的结果,如下所示:

code | date1 | date2

a | 2016-04-19  | 2016-04-21 |

b | 2016-04-13  | 2016-04-14 |

我想生成date1和date2之间日期范围的每一天,如下所示:

代码| date_result

a | 2016-04-19

a | 2016-04-20

a | 2016-04-21

b | 2016-04-13

b | 2016-04-14

我找到了一个查询示例,它生成两个日期范围之间的每个日期,如下所示:

SELECT ADDDATE('2016-04-10', INTERVAL @i:=@i+1 DAY) AS DAY
FROM (
SELECT a.a
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
JOIN (SELECT @i := -1) r1
WHERE 
@i < DATEDIFF('2016-04-19', '2016-04-10')

但我不能用我的查询来实现它:(

3 个答案:

答案 0 :(得分:1)

您可以使用from_days()将日期转换为日期数字 然后使用tally表进行内连接(顺序号从1开始) 编号730485是&#39; 2000-01-01&#39; offset(select from_days(&#39; 2000-01-01&#39;))

select a.* , from_days(t.tallyid+730485) from 
(
    select 'a' code , '2016-04-19' date1,  '2016-04-21' date2
    union all
    select 'b'code , '2016-04-13' date1,  '2016-04-14' date2
) a
inner join Tally t on t.tallyid between (TO_DAYS(a.date1)-730485) and (TO_DAYS(a.date2)-730485)

答案 1 :(得分:0)

这是单个查询:

select a.* , from_days(t.tallyid+730485) from 
(
    select 'a' code , '2016-04-19' date1,  '2016-04-21' date2
    union all
    select 'b'code , '2016-04-13' date1,  '2016-04-14' date2,
) a
left join 
(
    SELECT @row := @row + 1 as tallyid FROM 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t,
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2, 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3, 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t4, 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t5, 
    (SELECT @row:=0) a
) t on t.tallyid between (TO_DAYS(a.date1)-730485) and (TO_DAYS(a.date2)-730485)
在子查询中创建

从1到100000的序列,它适用于2000年到2237年之间的日期。

答案 2 :(得分:0)

最后我找到了从date1到date2生成日期的答案:

ArrayList<LatLng> locations = new ArrayList();
locations.add(new LatLng(30.243442, -1.432320));
locations.add(new LatLng(... , ...));
.
.
.

for(LatLng location : locations){
 mMap.addMarker(new MarkerOptions()
    .position(location)
    .title(...)
 }

感谢你们的赞赏