mysql - 如何使用 node.js 在 mySQL 中进行批量更新

标签 mysql node.js

我想批量更新数据 我有超过 50 行要在 Node JS 的对象数组中更新。就像是 https://github.com/felixge/node-mysqlHow do I do a bulk insert in mySQL using node.js

var updateData=[ 
    {a: '15',b: 1,c: '24',d: 9,e: 1,f: 0,g: 0,h: 5850,i: 78 },
    {a: '12',b: 1,c: '21',d: 9,e: 1,f: 0,g: 0,h: 55,i: 78 },
    {a: '13',b: 1,c: '34',d: 9,e: 1,f: 0,g: 0,h: 58,i: 78 },
    {a: '14',b: 1,c: '45',d: 9,e: 1,f: 0,g: 0,h: 585,i:78 },
    {a: '16',b: 1,c: '49',d: 9,e: 1,f: 0,g: 0,h: 85,i: 78 } 
]
my query is : update table set a= updateData.a ,b= updateData.b ,c = updateData.c , d==updateData.d ,e=updateData.e,f=updateData.f where e=updateData.e

最佳答案

据我所知,在 mySQL 中没有直接的方法来批量更新记录。但是有一个解决方法 - 您可以执行多个插入语句,然后执行查询以获得所需的结果。

为此,在创建连接时允许它执行多个语句,因为默认情况下它是禁用的。

var connection = mysql.createConnection({
          host     : dbConfig.host,
          user     : dbConfig.user,
          password : dbConfig.password,
          database : dbConfig.database,
          multipleStatements: true
 });

然后,通过操作您拥有的输入,使用以下语法构造批量更新查询。

Query1; Query2; Query3;

比如说,

 update table set a='15', b=1, c='24', d=9, e=1, f=0, g=0, h=5850, i=78;update table set a='12', b=1, c='21', d=9, e=1, f=0, g=0, h=5850, i=78;

然后,像往常一样执行查询,

connection.query(sqlQuery, params, callback);

希望这对您有所帮助。

关于mysql - 如何使用 node.js 在 mySQL 中进行批量更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37415464/

相关文章:

php - 获取 contenteditable div 的当前值并更新数据库

mysql - 如何在两个不同的表中强制执行不同的主键?

javascript - 如何在node.js REPL中禁用 "special commands"?

node.js - ./node_modules/@angular/animations/__ivy_ngcc__/fesm2015/browser.js 中的错误

javascript - 如何在 vscode 中查看全 Angular 代码

node.js - OCaml/Node.JS 上的 Lwt.async 和 Lwt_main.run 有什么区别?

python - MySQL 到云存储桶 Airflow DAG 的 UnicodeDecodeError

mysql - 如何将地理位置查询与其他条件结合起来

java - 如何在hibernate中使用命名查询访问子表字段

Node.js 同时读写流到同一个文件