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

问题描述 投票:0回答:4

在node.js中使用felixge的mysql时,如何向结果对象询问返回的行数?我有一个相当昂贵的查询,所以我不想先运行

COUNT(*)
,只是为了第二次运行查询。

mysql node.js
4个回答
65
投票

如果是select查询,则取返回数组的长度即可。

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

如果是更新/删除查询,返回的字典将有一个affectedRows变量。

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

1
投票

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


0
投票

也许是最简单的语法:

const [results, fields] = await your_pool.execute(
    `delete from sync where n = ? and a = ? and s = ?`,
    [name, age, sex])

console.log("count deleted ..", results.affectedRows)

别忘了..在开发过程中

console.log(results)
看看到底发生了什么:

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 2,
  insertId: 0,
  info: '',
  serverStatus: 2,
  warningStatus: 0,
  changedRows: 0
}

-2
投票

截至2015年4月13日的mssql 2.1.2版本:

从 DeviceAccountLinks 中删除 其中 DeviceAccountId = @deviceAccountId 和设备类型 = @deviceType

语句不会产生“未定义”的结果

我已将声明更改为:

从 DeviceAccountLinks 中删除 其中 DeviceAccountId = @deviceAccountId 和设备类型 = @deviceType; 选择@@rowcount“rowCount”

获取输出:[{rowCount:1}]

© www.soinside.com 2019 - 2024. All rights reserved.