比如:
update wealth_table set coin = coin + 100 where id = 101;
update wealth_table set coin = coin + 200 where id = 102;如何把这两条语句合并成一条?

解决方案 »

  1.   

    update wealth_table set coin = coin + if(id = 101,100,if(id = 102,200,0))
      

  2.   

    update wealth_table set coin=coin+(id-100)*100 where id in (101,102);
      

  3.   

    mysql允许一次update多少了,像楼上那样的话,我一次update超过200个字段效率上会不会很低呢?
      

  4.   

    update wealth_table set coin = coin + 
    case id
    when 101 then 100
    when 102 then 200
    else 0 end
      

  5.   


    虽然这个方法对于连续id很有效,但是我这个id不一定是连续的哈,
      

  6.   


    如果case多了(200+),效率会不会很低呢?
      

  7.   

    CASE多了肯定有效率上的影响。 除非你的值有规则,比如set coin = coin + (id-100)*100