mysql 批量修改表中欄位資料成不同的值 用一個表更新另一個表的方法
阿新 • • 發佈:2019-02-01
首先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】則具有通用型也不具結構隱患 、
Solution 1: 修改1列
update student s, city c
set s.city_name = c.name
where s.city_code = c.code;
Solution 2: 修改多個列
update a, b
set a.title=b.title, a.name=b.name
where a.id=b.id
Solution 3: 採用子查詢
update student s set city_name = (select name from city where code = s.city_code);
這3種方式都是採用中間表的方式來實現的,因為資料量小,我就先把主表的資料(`id`,`longitude`,`latitude`) 先匯出到Excel表格,在Excel修改好了資料然後倒入資料庫裡建立的臨時表,然後用上面的方法去跟新主表的資料。 以上內容來源於網路,僅僅留作日後自身參考學習使用,如果大家有更好的方法歡迎留言指教。