对于下面显示的表格的每一行,我正在尝试按时间顺序创建rate_id出现的次数,直到创建时间为止。
conversions | rate_id | created | valid | count | +---------------------------------------------------+ + 1 | 2016-07-01 11:23:00 | 1 | | + 2 | 2016-07-02 18:58:39 | 1 | | + 2 | 2016-07-02 19:49:20 | 1 | | + 3 | 2016-07-03 18:58:39 | 1 | |
会导致
conversions | rate_id | created | valid | count | +---------------------------------------------------+ + 1 | 2016-07-01 11:23:00 | 1 | 1 | + 2 | 2016-07-02 18:58:39 | 1 | 1 | + 2 | 2016-07-02 19:49:20 | 1 | 2 | + 3 | 2016-07-03 18:58:39 | 1 | 1 |
我已尝试运行以下MySQL查询,但我觉得必须有更有效的方法来执行此操作并感谢您的想法和建议:
update conversions a
set a.count = (select sum(valid) from conversions c where c.created <= a.created and c.rate_id = a.rate_id)
where date(a.created) > '2016-05-31';