search

Home  >  Q&A  >  body text

php - mysql根据不同条件修改多条记录的值

update category
set sort=1 where id=1,
set sort=2 where id=5,
set sort=3 where id=12
·····
这么写会报错,请问类似这样的需求,有没有什么简单的写法?
还是只能
update ··· where id=1
update ··· where id=2
·····

阿神阿神2933 days ago487

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-10 17:59:01

    case就可以实现拉

    UPDATE category set sort=(case 
        when id=1 then 1 
        when id=5 than 2
        when id=12 than 3
        end)

    reply
    0
  • Cancelreply