我有一个名为bug_history_table的表,如下所示
------------------------------------------------------------------------
| bug_id | date_modified | field_changed | old_value | new_value |
------------------------------------------------------------------------
| 14415 | 2015-11-03 13:52:32 | status | unconfirm | open |
| 14415 | 2015-11-04 10:50:58 | status | open | resolved |
| 14415 | 2015-11-10 11:27:41 | status | resolved | verified |
| 14415 | 2015-11-14 11:27:41 | status | verified | closed |
------------------------------------------------------------------------
上表显示状态的错误历史记录从打开更改为关闭。我想在错误空闲时显示两个日期之间的结果,即状态在修改之前保持不变。
预期产出 -
--------------------------------------
| bug_id | date_modified | new_value |
--------------------------------------
| 14415 | 2015-11-03 | open |
| 14415 | 2015-11-04 | resolved |
| 14415 | 2015-11-05 | resolved | <---
| 14415 | 2015-11-06 | resolved | <---
| 14415 | 2015-11-07 | resolved | <--- Need these extra rows.
| 14415 | 2015-11-08 | resolved | <---
| 14415 | 2015-11-09 | resolved | <---
| 14415 | 2015-11-10 | verified |
| 14415 | 2015-11-11 | verified | <---
| 14415 | 2015-11-12 | verified | <--- Need these extra rows.
| 14415 | 2015-11-13 | verified | <---
| 14415 | 2015-11-14 | closed |
--------------------------------------
仅在修改错误状态时插入条目。现在让我们举例如下 -
如果错误是&#34;打开&#34;在2015-11-03,然后没有用户更新其状态,直到2015-11-06,这意味着错误仍然闲置3天。所以我想显示那个错误在&#34;打开&#34;这些天2015-11-03,2015-11-04,2015-11-05的状态。
答案 0 :(得分:1)
制作此解决方案的最简单方法是使用一个过程,创建一个您需要的日期表,并使用此表保持连接。
drop temporary table if exists tmp_dates;
create temporary table tmp_dates(_date timestamp);
set @tmp_date = start_date;
while @tmp_date <= end_date do
insert into tmp_dates values (@tmp_date);
set @tmp_date = @tmp_date+interval 1 day;
end while;
select a._date,b.bug_id,ifnull(b.new_value,'resolved',b.new_value) from tmp_dates a
left join bug_history_table b on a._date = b.date_modified;
答案 1 :(得分:-1)