Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select mysql COUNT value in nodejs

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  '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?

>Solution :

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'
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading