[英]select mysql COUNT value in nodejs
Mysql query looks like this Mysql查询看起来像这样
const numOfReplies = await SQL('SELECT COUNT(conversation_id), conversation_id FROM tweet_replies GROUP BY conversation_id')
so there are multiple rows with the same conversation_id, and i want to get a number of rows for every conversation_id.所以有多行具有相同的conversation_id,我想为每个conversation_id 获取多行。 After looping with forEach
用 forEach 循环后
numOfReplies.forEach(num => {
console.log(num)
})
response looks like this响应看起来像这样
{
'COUNT(conversation_id)': '10',
conversation_id: '1530169643423367169'
}
{
'COUNT(conversation_id)': '10',
conversation_id: '1530172022357106690'
}
how can i select value of COUNT(conversation_id) from this response?如何从此响应中选择 COUNT(conversation_id) 的值?
Simply name the COUNT value in SQL request with AS
keyword and get it like a field :只需使用
AS
关键字命名 SQL 请求中的 COUNT 值,然后像字段一样获取它:
SELECT COUNT(conversation_id) AS myCount, conversation_id FROM tweet_replies GROUP BY conversation_id
You will get response like this:你会得到这样的回应:
{
myCount: '10',
conversation_id: '1530169643423367169'
}
{
myCount: '10',
conversation_id: '1530172022357106690'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.