前言
近期在刷新生產環境數據庫的時候,需要更新表中的字段,如果對每條數據結果都執行一次update語句,占用的數據庫資源就會很多,而且速度慢。
因為項目是laravel框架,laravel有批量插入的方法,卻沒有批量更新的方法,沒辦法只能自己實現。
準備
mysql case…when的用法
mysql 的 case when 的語法有兩種:
簡單函數
case [col_name] when [value1] then [result1]…else [default] end
case [col_name] when [value1] then [result1]…else [default] end: 枚舉這個字段所有可能的值
1
2
3
4
5
6
7
|
select id,status '狀態值' , case status when 10 then '未開始' when 20 then '配送中' when 30 then '已完成' when 40 then '已取消' end '狀態' from table |
輸出結果:
搜索函數
case when [expr] then [result1]…else [default] end
case when [expr] then [result1]…else [default] end:搜索函數可以寫判斷,并且搜索函數只會返回第一個符合條件的值,其他case被忽略
1
2
3
4
5
|
select id,lessee_id '租戶id' , case when lessee_id <=1 then '自用系統' when lessee_id >1 then '租用系統' end '系統分類' from waybill_base_info |
case…when實現數據庫的批量更新
更新單列的值
1
2
3
4
5
6
7
|
update base_info set city_id = case id when 1 then when 2 then when 3 then end where id in (1,2,3) |
這句sql的意思是,更新city_id 字段:
如果id=1 則city_id 的值為100010,
如果id=2 則 city_id 的值為100011,
如果id=3 則 city_id 的值為100012。
即是將條件語句寫在了一起。
這里的where部分不影響代碼的執行,但是會提高sql執行的效率。
確保sql語句僅執行需要修改的行數,這里只有3條數據進行更新,而where子句確保只有3行數據執行。
更新多列的值
1
2
3
4
5
6
7
8
9
10
11
12
|
update base_info set city_id = case id when 1 then 100010 when 2 then 100011 when 3 then 100012 end , city_name = case id when 1 then ‘北京' when 2 then ‘上海' when 3 then ‘廣州' end where id in (1,2,3) |
不過這個有個缺點 : 要注意的問題是sql語句的長度,需要考慮程序運行環境所支持的字符串長度,當然這也可以更新mysql的設置來擴展。
laravel實現批量更新
在model方法中封裝該批量更新的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
//批量更新 public function updatebatch( $multipledata = []) { try { if ( empty ( $multipledata )) { log::info( "批量更新數據為空" ); return false; } $tablename = $this ->table; // 表名 $firstrow = current( $multipledata ); $updatecolumn = array_keys ( $firstrow ); // 默認以id為條件更新,如果沒有id則以第一個字段為條件 $referencecolumn = isset( $firstrow [ 'id' ]) ? 'id' : current( $updatecolumn ); unset( $updatecolumn [0]); // 拼接sql語句 $updatesql = "update " . $tablename . " set " ; $sets = []; $bindings = []; foreach ( $updatecolumn as $ucolumn ) { $setsql = "`" . $ucolumn . "` = case " ; foreach ( $multipledata as $data ) { $setsql .= "when `" . $referencecolumn . "` = ? then ? " ; $bindings [] = $data [ $referencecolumn ]; $bindings [] = $data [ $ucolumn ]; } $setsql .= "else `" . $ucolumn . "` end " ; $sets [] = $setsql ; } $updatesql .= implode( ', ' , $sets ); $wherein = collect( $multipledata )->pluck( $referencecolumn )->values()->all(); $bindings = array_merge ( $bindings , $wherein ); $wherein = rtrim( str_repeat ( '?,' , count ( $wherein )), ',' ); $updatesql = rtrim( $updatesql , ", " ) . " where `" . $referencecolumn . "` in (" . $wherein . ")" ; log::info( $updatesql ); // 傳入預處理sql語句和對應綁定數據 return db::update( $updatesql , $bindings ); } catch (\exception $e ) { return false; } } |
在service層拼接需要更新的數據,并調用該函數:
1
2
3
4
5
6
7
8
9
10
|
foreach ( $taskinfo as $info ) { $cityid = $info [ 'requirement' ][ 'city_ids' ]; //此處省略n行代碼 $cityinfo = [ 'id' => $dataid [ $info [ 'id' ]][ 'id' ], 'city_id' => $cityid ]; if ( $cityinfo ) { $cityinfos [] = $cityinfo ; } } $res = $this ->waybilldriverinfomodel->updatebatch( $cityinfos ); } |
拼接的批量更新的數組格式為:
$students = [
[‘id' => 1, ‘city_id' => ‘100010'],
[‘id' => 2, ‘city_id' => ‘100011'],
];
生成的sql語句如下:
1
|
update base_info set `city_id` = case when `id` = 1 then 100010 when `id` = 2 then 100011 else `city_id` end where `id` in (1,2) |
因為每次只操作20條數據,所以這樣拼接的字符串不會太長,符合mysql的字符串長度的要求,解決問題。
本文主要講解了laravel實現批量更新多條數據的方法,更多關于laravel的使用技巧請查看下面的相關鏈接
原文鏈接:https://blog.csdn.net/qq_28673091/article/details/100534908