我如何使用node.js在MySQL中进行批量更新 [英] How do I do a bulk update in mySQL using node.js

查看:434
本文介绍了我如何使用node.js在MySQL中进行批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想批量更新数据,我有50多个行要在节点JS中的对象数组中更新.就像是 https://github.com/felixge/node-mysql 我如何使用node.js在MySQL中进行批量插入

i want to update data in bulk i have over 50 rows to be updated in an array of objects in node JS. something like https://github.com/felixge/node-mysql and How 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中进行批量更新记录.但是,有一种解决方法-您可以执行多个insert语句,然后执行查询以获得所需的结果.

As of I know, there is no direct way to do bulk update records in mySQL. But there is a work around for this - You could execute multiple insert statements and then execute the query to achieve the desired result.

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

To do this, while creating a connection allow it to execute multiple statements as it is disabled by default.

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

然后,通过操纵您的输入,以以下语法构造批量更新查询.

Then, construct the bulk update query in the below syntax by manipulating the inputs you have.

Query1; Query2; Query3;

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;

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

Then, execute the query as usual,

connection.query(sqlQuery, params, callback);

希望这会有所帮助.

这篇关于我如何使用node.js在MySQL中进行批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆