如何将“count(*)”值存储在NodeJS的variables中?
我想在MySQL查询中存储count(例如: – 5)的结果给一个variables,但它将整个JSOstring存储为[{"COUNT(*)":8}]
,所以如何存储值8
我的variables行?
我在NodeJS中的代码:
connection.query('SELECT COUNT(*) FROM issues', function(err, rows){ var row=JSON.stringify(rows); console.log(row); });
使用sql别名:
connection.query('SELECT COUNT(*) AS count FROM issues', (err, rows) => { const count = rows[0].count; // const count = rows[0]['COUNT(*)']; // without alias console.log(`count: ${count}`); });
只需通过键从行对象中提取值即可。 rows[0]['COUNT(*)']
应该工作。 这样做之前,你把它串起来。
编辑:正如下面的评论指出, rows
是一个数组。 编辑答案。