【问题标题】:How do I do a bulk update in mySQL using node.js如何使用 node.js 在 mySQL 中进行批量更新
【发布时间】:2016-09-21 18:41:14
【问题描述】:

我想批量更新数据我在节点 JS 的对象数组中有超过 50 行要更新。就像是 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 node.js


【解决方案1】:

您可以通过在 mysql 连接中启用多语句功能来完成此操作。然后你可以遍历你的 updateData 并构造用';'分隔的 mysql 语句。你可以看到这个in this answer的例子。

【讨论】:

    【解决方案2】:

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

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

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

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

    查询1;查询2;查询3;

    比如说,

     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);
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      使用 node-MySQL 批量更新数据确实不容易,但如果你可以在前端使用 .map 函数,你可以做一个替代方案。让我告诉你我用我的做了什么--

      只需制作一个更新 API 并在您的前端像这样使用它-

       updateData.map((item, key)=>{
          return (
              axios.path('/api/update', {
                  a: item.a,
                  b: item.b,
                  c: item.c
              })
          ).then(()=> console.log('updated'))
          .catch((err)=> console.log(err))
      })
      

      【讨论】:

        【解决方案4】:

        回答有点晚,但使用 MySQL JSON_TABLE 会有所帮助。这是一个工作示例:

        UPDATE person a
        INNER JOIN (
            SELECT
                personId, addressType, addressId 
            FROM JSON_TABLE('
                [
                    {"personId": 318, "addressType": "Primary", "addressId": 712},
                    {"personId": 319, "addressType": "Shipping", "addressId": 712}
                ]',
                '$[*]' COLUMNS(
                    personId INT PATH '$.personId',
                    addressType VARCHAR(10) path '$.addressType', 
                    addressId INT path '$.addressId')
            ) a) b
        ON a.personId = b.personId
        SET
            a.addressId = b.addressId,
            a.addressType = b.addressType;
        

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 2012-02-12
        • 1970-01-01
        • 2012-07-10
        • 2012-09-21
        • 1970-01-01
        • 2018-04-01
        • 2020-07-10
        • 1970-01-01
        相关资源
        最近更新 更多