我有一个表aa,里边有三个字段id,shop_id,sort_id 
表里边有些数据 
1,1,0 
2,1,0 
3,2,0 
4,2,0 
我现在怎么能把sort_id更新为这样的呢? 
1,1,1 
2,1,2 
3,2,1 
4,2,2

解决方案 »

  1.   

    查询:select 
      id,
      shop_id,
      (select count(1) from aa where shop_id=t.shop_id and id<t.id)+1 as sort_id
    from aa t
      

  2.   

    1.
    我来写更新吧。
    ^^
    update aa as a,
    (
    select tt.id,(select count(*)+1 from aa where shop_id = tt.shop_id and id< tt.id) as c 
    from aa as tt
    ) as b 
    set a.short_id = b.c where a.id = b.id;
      

  3.   

    update aa set short_id =(id%2+1); 
    不知道可以嗎?
      

  4.   

    update aa set short_id =(id%2+1); 
    不知道可以嗎?
      

  5.   

    楼上兄弟的方法是可以的 只是稍微有点错update aa set short_id=((id+1)%2+1);