【问题标题】:mysql cursor update position index with loop countmysql游标更新位置索引与循环计数
【发布时间】:2017-03-23 23:12:32
【问题描述】:

卡车详细信息表

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 更新位置索引值,如表中所示。每个订单将有多种卡车类型。如果一辆卡车重复订购 2 次,则位置索引将为 1 、 2。 那么任何人都可以帮我解决这个问题... 我尝试使用光标.. 不是位置索引没有正确更新

【问题讨论】:

    标签: mysql loops cursor


    【解决方案1】:

    这很容易在没有光标的情况下完成。你只需要变量:

    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;
    

    【讨论】:

    • 你能解释一下案件情况吗,我不介意
    • @user2848775 。 . . case 条件正在设置变量。 ot 正在跟踪每个组。第一个条件检查组是否相同;如果是这样,它会增加@rn。第二个条件将组设置为当前组并将@rn设置为1。
    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多