我有下表及其活动时间的项目表(期间由FROM和TO日期定义):
ID | ProjID | ActiveFrom | ActiveTo
===+========+============+============
1 | 20 | 2018-01-01 | 2018-01-04
2 | 20 | 2018-02-05 | 2018-02-07
3 | 20 | 2018-02-20 | 2018-02-22
4 | 30 | 2018-01-15 | 2018-02-15
项目有任意数量的活动周期。
我需要一个查询,该查询将返回给定项目的所有活动日期。例如对于ProjID = 20,结果应为:
2018-01-01
2018-01-02
2018-01-03
2018-01-04
2018-02-05
2018-02-06
2018-02-07
2018-02-20
2018-02-21
2018-02-22
答案 0 :(得分:0)
您是这样说的吗?
SET @row_number = 0;
SELECT DATE_ADD(DATE_ADD(ActiveFrom, INTERVAL -1 DAY), INTERVAL x.D DAY) AS ActivityPeriods
FROM test
LEFT JOIN(SELECT *, (@row_number:=@row_number + 1) AS D FROM information_schema.COLUMNS) AS x ON x.D <= DATEDIFF(ActiveTo,ActiveFrom) +1
WHERE
ProjID = 20
ORDER BY ActivityPeriods
这会给你:
| ActivityPeriods |
| --------------- |
| 2018-01-01 |
| 2018-01-02 |
| 2018-01-03 |
| 2018-01-04 |
| 2018-02-05 |
| 2018-02-06 |
| 2018-02-07 |
| 2018-02-20 |
| 2018-02-21 |
| 2018-02-22 |