find number of rows in returned mysql result (nodejs)

后端 未结 3 1639
没有蜡笔的小新
没有蜡笔的小新 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:46

    With the version of mssql 2.1.2 as of 2015-04-13:

    delete from DeviceAccountLinks where DeviceAccountId = @deviceAccountId and DeviceType = @deviceType

    statement will produce no results as 'undefined'

    I have changed the statement to:

    delete from DeviceAccountLinks where DeviceAccountId = @deviceAccountId and DeviceType = @deviceType; select @@rowcount "rowCount"

    to get the output of: [{rowCount:1}]

    0 讨论(0)
  • 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;
    });
    
    0 讨论(0)
  • 2020-12-06 04:58

    If you're using the examples in the readme just look at the length property of the rows object (i.e. rows.length).

    0 讨论(0)
提交回复
热议问题