我想跟踪以前的记录日期,我正在寻找一种创建视图或创建新的先前日期列的方法。
样本表:
select id, previous, current
from reports
where prod_id = 25
order by id desc;
+------+---------------+---------------+
| id | previous | current |
+------+---------------+---------------+
| 229 | 0000-00-00 | 2018-03-19 |
| 208 | 0000-00-00 | 2018-01-05 |
| 134 | 0000-00-00 | 2017-08-22 |
+------+---------------+---------------+
阅读其他主题后,我尝试了以下方法:
select id, current, @previous:= current as previous
from reports join (select @previous := 0) dummy
where prod_id = 25
order by id desc;
+------+---------------+---------------+
| id | current | previous |
+------+---------------+---------------+
| 229 | 2018-03-19 | 2018-03-19 |
| 208 | 2018-01-05 | 2018-01-05 |
| 134 | 2017-08-22 | 2017-08-22 |
+------+---------------+---------------+
在第二个示例中,先前和当前仍然相同。虽然我希望表格是这样的:
+------+---------------+---------------+
| id | previous | current |
+------+---------------+---------------+
| 229 | 2018-01-05 | 2018-03-19 |
| 208 | 2017-08-22 | 2018-01-05 |
| 134 | 2017-06-14 | 2017-08-22 |
+------+---------------+---------------+
我试图找出我的示例sql中缺少的内容,该示例未获取以前的记录当前日期。然后将其更改为更新。