Home > database >  select mysql COUNT value in nodejs
select mysql COUNT value in nodejs

Time:06-02

Mysql query looks like this

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. After looping with 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?

CodePudding user response:

Simply name the COUNT value in SQL request with AS keyword and get it like a field :

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'
}
  • Related