Home >Backend Development >PHP Tutorial >mysql根据不同条件修改多条记录的值

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:06:141945browse

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
·····

回复内容:

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
·····

case就可以实现拉

<code class="sql">UPDATE category set sort=(case 
    when id=1 then 1 
    when id=5 than 2
    when id=12 than 3
    end)</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn