是否有类似 WHERE IN 的语法允许我一次更新多个值? 示例:
update files
set name = 'untitled'
WHERE id IN (1,2,3,4)
成为:
update files
set name ( 'untitled', 'untitled2', 'untitled3', 'untitled4' )
WHERE id IN (1,2,3,4)
我的脚本包含一个关联数组,我需要更新设置为数组值的名称列,其中 id 列与数组键匹配
最佳答案
您在寻找 case
语句吗?
update files
set name = (case when id = 1 then 'untitled'
when id = 2 then 'untitled2'
when id = 3 then 'untitled3'
when id = 4 then 'untitled4'
end)
where id IN (1, 2, 3, 4);
在 MySQL 中,您还可以使用 join
来执行此操作:
update files f join
(select 1 as id, 'untitled' as newname union all
select 2, 'untitled2' union all
select 3, 'untitled3' union all
select 4, 'untitled4'
) n
on f.id = n.id
f.name = new.newname;
如果您有很多值,您可以单独创建一个包含这些值的表格,然后进行更新。
关于php - MySQL - 更新多个值和 WHERE IN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340135/