mysql批量更新多条记录的同一个字段为不同值的方法
程序员文章站
2023-12-10 18:17:04
首先mysql更新数据的某个字段,一般这样写:
update mytable set myfield = 'value' where other_field =...
首先mysql更新数据的某个字段,一般这样写:
update mytable set myfield = 'value' where other_field = 'other_value';
也可以这样用in指定要更新的记录:
update mytable set myfield = 'value' where other_field in ('other_values');
这里注意 ‘other_values' 是一个逗号(,)分隔的字符串,如:1,2,3
如果更新多条数据而且每条记录要更新的值不同,可能很多人会这样写:
foreach ($values as $id => $myvalue) { $sql = "update mytable set myfield = $myvalue where id = $id"; mysql_query($sql); }
即是循环一条一条的更新记录。一条记录update一次,这样性能很差,也很容易造成阻塞。
那么能不能一条sql语句实现批量更新呢?
mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现。
update mytable set myfield = case id when 1 then 'myvalue1' when 2 then 'myvalue2' when 3 then 'myvalue3' end where other_field ('other_values')
如果where条件查询出记录的id不在case范围内,myfield将被设置为空。
如果更新多个值的话,只需要稍加修改:
update mytable set myfield1 = case id when 1 then 'myvalue11' when 2 then 'myvalue12' when 3 then 'myvalue13' end, myfield2 = case id when 1 then 'myvalue21' when 2 then 'myvalue22' when 3 then 'myvalue23' end where id in (1,2,3)
这里以php为例,构造这两条mysql语句:
1、更新多条单个字段为不同值, mysql模式
$ids_values = array( 1 => 11, 2 => 22, 3 => 33, 4 => 44, 5 => 55, 6 => 66, 7 => 77, 8 => 88, ); $ids = implode(',', array_keys($ids_values )); $sql = "update mytable set myfield = case id "; foreach ($ids_values as $id=> $myvalue) { $sql .= sprintf("when %d then %d ", $id, $myvalue); } $sql .= "end where id in ($ids)"; echo $sql.";<br/>";
输出:
update mytable set myfield = case id when 1 then 11 when 2 then 22 when 3 then 33 when 4 then 44 when 5 then 55 when 6 then 66 when 7 then 77 when 8 then 88 end where id in (1,2,3,4,5,6,7,8);
2、更新多个字段为不同值, pdo模式
$data = array(array('id' => 1, 'myfield1val' => 11, 'myfield2val' => 111), array('id' => 2, 'myfield1val' => 22, 'myfield2val' => 222)); $where_in_ids = implode(',', array_map(function($v) {return ":id_" . $v['id'];}, $data)); $update_sql = 'update mytable set'; $params = array(); $update_sql .= ' myfield1 = case id'; foreach($data as $key => $item) { $update_sql .= " when :id_" . $key . " then :myfield1val_" . $key . " "; $params[":id_" . $key] = $item['id']; $params[":myfield1val_" . $key] = $item['myfield1val']; } $update_sql .= " end"; $update_sql .= ',myfield2 = case id'; foreach($data as $key => $item) { $update_sql .= " when :id_" . $key . " then :myfield2val_" . $key . " "; $params[":id_" . $key] = $item['id']; $params[":myfield1va2_" . $key] = $item['myfield2val']; } $update_sql .= " end"; $update_sql .= " where id in (" . $where_in_ids . ")"; echo $update_sql.";<br/>"; var_dump($params);
输出:
update mytable set myfield1 = case id when :id_0 then :myfield1val_0 when :id_1 then :myfield1val_1 end,myfield2 = case id when :id_0 then :myfield2val_0 when :id_1 then :myfield2val_1 end where id in (:id_1,:id_2); array (size=6) ':id_0' => int 1 ':myfield1val_0' => int 11 ':id_1' => int 2 ':myfield1val_1' => int 22 ':myfield1va2_0' => int 111 ':myfield1va2_1' => int 222
另外三种批量更新方式
1. replace into 批量更新
replace into mytable(id, myfield) values (1,'value1'),(2,'value2'),(3,'value3');
2. insert into ...on duplicate key update批量更新
insert into mytable(id, myfield1, myfield2) values (1,'value11','value21'),(2,'value12','value22'),(3,'value13','value23') on duplicate key update myfield1=values(myfield2),values(myfield2)+values(id);
3. 临时表
drop table if exists `tmptable`; create temporary table tmptable(id int(4) primary key,myfield varchar(50)); insert into tmptable values (1,'value1'),(2,'value2'),(3,'value3'); update mytable, tmptable set mytable.myfield = tmptable.myfield where mytable.id = tmptable.id;
- 【replace into】和【insert into】更新都依赖于主键或唯一值,并都可能造成新增记录的操作的结构隐患
- 【replace into】操作本质是对重复记录先delete然后insert,如果更新的字段不全缺失的字段将被设置成缺省值
- 【insert into】则只是update重复的记录,更改的字段只能依循公式值
- 【临时表】方式需要用户有temporary 表的create 权限
- 数量较少时【replace into】和【insert into】性能最好,数量大时【临时表】最好,【case】则具有通用型也不具结构隐患
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。