我有以下情况:
+------------+-----------+------------+----------------+
| date | event | start_date | duration(days) |
+------------+-----------+------------+----------------+
| 2010-01-01 | concert | 2010-01-01 | 2 |
+------------+-----------+------------+----------------+
| 2010-01-02 | NULL | NULL | NULL |
+------------+-----------+------------+----------------+
| 2010-01-03 | workshops | 2010-01-03 | 3 |
+------------+-----------+------------+----------------+
| 2010-01-04 | NULL | NULL | NULL |
+------------+-----------+------------+----------------+
| 2010-01-05 | NULL | NULL | NULL |
+------------+-----------+------------+----------------+
| 2010-01-06 | NULL | NULL | NULL |
+------------+-----------+------------+----------------+
| 2010-01-07 | NULL | NULL | NULL |
+------------+-----------+------------+----------------+
| 2010-01-08 | festival | 2010-01-08 | 5 |
+------------+-----------+------------+----------------+
| 2010-01-09 | NULL | NULL | NULL |
+------------+-----------+------------+----------------+
现在,我需要以特定事件发生时每个日期行的方式显示上述信息,其名称显示在事件列中,而不仅仅是在它开始的第一天。
有三个表,一个只有 DATES ,一个有 EVENTS ,另一个有 START_DATES 和 DURATION 每个事件。
到目前为止,我只能生成一个像上面这样的表,但我不知道如何在事件持续时间的每一天用事件名替换NULL值。
SELECT d AS date, event, start_date, duration
FROM dates
LEFT JOIN info ON info.start_date=d.date AND city='LA'
LEFT JOIN events ON events.event=info.event
WHERE d BETWEEN '2010-01-01' AND '2010-01-09'
有什么想法吗?
答案 0 :(得分:0)
下面 - 创建了一个子查询,显示所有事件的开始日期和结束日期,然后加入完整的日期列表:
SELECT d.date, t.event, t.start_date, t.end_date
FROM dates d
LEFT JOIN (SELECT e.event, i.start_date, i.start_date + INTERVAL i.duration DAY AS end_date
FROM events e
JOIN info i
ON e.event = i.event
WHERE i.city = 'LA'
) t
ON d.date BETWEEN t.start_date AND t.end_date
WHERE d.date BETWEEN '2010-01-01' AND '2010-01-09'
ORDER BY d.date, e.event_name;