一条sql语句update 多条记录
Posted 三七、
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一条sql语句update 多条记录相关的知识,希望对你有一定的参考价值。
UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END, title = CASE id WHEN 1 THEN ‘New Title 1‘ WHEN 2 THEN ‘New Title 2‘ WHEN 3 THEN ‘New Title 3‘ END WHERE id IN (1,2,3)
$display_order = array( 1 => 4, 2 => 1, 3 => 2, 4 => 3, 5 => 9, 6 => 5, 7 => 8, 8 => 9 ); $ids = implode(‘,‘, array_keys($display_order)); $sql = "UPDATE categories SET display_order = CASE id "; foreach ($display_order as $id => $ordinal) { $sql .= sprintf("WHEN %d THEN %d ", $id, $ordinal); // 拼接SQL语句 } $sql .= "END WHERE id IN ($ids)"; echo $sql; mysql_query($sql);
以上是关于一条sql语句update 多条记录的主要内容,如果未能解决你的问题,请参考以下文章