Batch updates of mysql update in nodejs

batch updates of mysql update in nodejs

is passed in an array, and the former affects the latter
for example, [id,num]
[[1jue 3], [2jue 5], [6je 8]]

.

SET num=? WHERE id=?

Mar.04,2021

[[1,3],[2,5],[6,8]].forEach((item,index)=>
    connection.query('update table SET num=? WHERE id=?',item,function(err,result)=>{
    
    })
)

connection pool multipleStatements is set to true, then several update statements are directly submitted into one sentence, and the fields after where are indexed. This makes it very fast for the three methods of mysql batch update in


node.js:
https://blog.csdn.net/huzhenv.


 $display_order     = [
   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 .= "END WHERE id IN ($ids)";
 echo $sql;

refer to the code of php and splice sql by yourself

Menu