mysql 游标用循环计数更新位置索引

mysql cursor update position index with loop count

卡车详细信息表

id  Order_ref_id    tryck_type_ref_id   position_index
1   226                 24                  1
2   226                 24                  2
3   226                 32                  1
4   226                 35                  1
5   226                 35                  2
6   227                 15                  1
7   227                 15                  2
8   228                 10                  1
9   229                 32                  1
10  229                 32                  2

mysql 更新位置索引值,如 table 中所示。每个订单将有多种卡车类型。如果一辆卡车重复订购 2 次,则位置索引将为 1、2。 那么任何人都可以帮助我解决这个问题...... 我试过使用 Cursor.. 不是位置索引没有正确更新

无需光标即可轻松完成此操作。您只需要变量:

set @rn := 0;
set @ot := ''
update t
    set position_index = (case when @ot = concat_ws('-', Order_ref_id, tryck_type_ref_id)
                               then (@rn := @rn + 1)
                               when @ot := concat_ws('-', Order_ref_id, tryck_type_ref_id)
                               then @rn := 1
                          end)
    order by id;