在返回的mysql结果中查找行数(nodejs) [英] find number of rows in returned mysql result (nodejs)

查看:49
本文介绍了在返回的mysql结果中查找行数(nodejs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用felixge的mysql作为node.js时,如何向结果对象询问返回的行数?我有一个相当昂贵的查询,所以我不想先运行COUNT(*),而后再运行一次查询.

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.

推荐答案

如果是选择查询,只需取返回数组的长度即可.

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

如果是更新/删除查询,则返回的字典将具有受影响的Rows变量.

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

这篇关于在返回的mysql结果中查找行数(nodejs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆