我有一个名为dateRange的表,看起来像这样
|ID|dateStart| dateEnd|
| 1|14-May-14|16-May-14|
| 1|20-May-14|21-May-14|
| 2|20-May-14|21-May-14|
我想在名为dateListed的表格中将其转换为以下格式
|ID| date|
| 1|14-May-14|
| 1|15-May-14|
| 1|16-May-14|
| 1|20-May-14|
| 1|21-May-14|
| 2|20-May-14|
| 2|21-May-14|
我已经回顾了以下问题Convert Date Range to Individual Days,但由于数据略有不同,我希望有人可以帮助我吗?
感谢。
答案 0 :(得分:1)
如果你有一个日期实用工具表,那么它很容易......
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(ID INT NOT NULL
,dateStart date NOT NULL
,dateEnd date NOT NULL
,PRIMARY KEY(ID,dateStart)
);
INSERT INTO my_table VALUES
(1,'2014-05-14','2014-05-16'),
(1,'2014-05-20','2014-05-21'),
(2,'2014-05-20','2014-05-21');
SELECT * FROM my_table;
+----+------------+------------+
| ID | dateStart | dateEnd |
+----+------------+------------+
| 1 | 2014-05-14 | 2014-05-16 |
| 1 | 2014-05-20 | 2014-05-21 |
| 2 | 2014-05-20 | 2014-05-21 |
+----+------------+------------+
SELECT x.ID
, c.dt date
FROM calendar c
JOIN my_table x
ON c.dt BETWEEN x.dateStart AND x.dateEnd
ORDER
BY ID
, date;
+----+------------+
| ID | date |
+----+------------+
| 1 | 2014-05-14 |
| 1 | 2014-05-15 |
| 1 | 2014-05-16 |
| 1 | 2014-05-20 |
| 1 | 2014-05-21 |
| 2 | 2014-05-20 |
| 2 | 2014-05-21 |
+----+------------+