find number of rows in returned mysql result (nodejs)

后端 未结 3 1645
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 04:42

When using felixge\'s mysql for node.js, how can I ask the result object for the number of returned rows? I have a rather expensive query so I don\'t want to run a COU

3条回答
  •  不知归路
    2020-12-06 04:58

    If it's a select query, just take the length of the returned array.

    connection.query(sql, [var1,var2], function(err, results) {
        numRows = results.length;
    });
    

    If it's an update/delete query, the returned dictionary will have an affectedRows variable.

    connection.query(sql, [var1,var2], function(err, result) {
        numRows = result.affectedRows;
    });
    

提交回复
热议问题