MySQL 在返回的mysql结果中查找行数(nodejs)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16199842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:27:16  来源:igfitidea点击:

find number of rows in returned mysql result (nodejs)

mysqlnode.js

提问by Huckle

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 COUNT(*)first, just to then run a query a second time.

node.js使用felixge的mysql时,如何向结果对象查询返回的行数?我有一个相当昂贵的查询,所以我不想运行COUNT(*)第一个,只是为了第二次运行查询。

回答by Daniel

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

回答by rdcapasso

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

如果您使用自述文件中的示例,只需查看行对象的长度属性(即rows.length)。

回答by vkhazin

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

使用 2015-04-13 的 mssql 2.1.2 版本:

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

从 DeviceAccountLinks 中删除,其中 DeviceAccountId = @deviceAccountId 和 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"

从 DeviceAccountLinks 中删除,其中 DeviceAccountId = @deviceAccountId 和 DeviceType = @deviceType;选择@@rowcount "rowCount"

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

得到输出:[{rowCount:1}]