2016-11-10 33 views
0

卡車詳細表MySQL的光標與循環更新位置索引計數

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的更新位置索引值如表I所示。每個訂單將有多種卡車類型。如果一輛卡車被重複訂購2次,那麼位置索引將爲1,2。 因此,任何人都可以幫助我... 我試過使用光標..沒有位置索引沒有正確更新

回答

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; 
+0

謝謝戈登·利諾夫其工作 – Pramod

+0

你能解釋的情況下條件,我對你不介意 – Pramod

+0

@ user2848775。 。 。 'case'條件是設置變量。 'ot'正在跟蹤每個組。第一個條件檢查組是否相同;如果是這樣,它會增加'@ rn'。第二個條件將該組設置爲當前組,並將'@ rn'設置爲1. –