0
votes

I use sequelize on my nodeJS app to query a sql database.

I try to sequelize this query but it doesn't work...

Can one help me ?

`SELECT content, COUNT(*) count FROM `hashtags` GROUP BY content ORDER BY count DESC LIMIT 10`

I try

Tags.findAll({group: ['content'], attributes: ['content', [sequelize.fn('COUNT', 'content'), 'content']],}).then(function(results) {

Tags is a sequelize define with date, content, id_tweet, id.

I have another query but I don't have any idea to do this because there are 2 inner join and count:

SELECT COUNT(*) AS nombredetweets, h.content, u.pseudo FROM tweets t INNER JOIN hashtags h ON t.id = h.id_tweet INNER JOIN user u ON t.id_user = u.id WHERE h.content = ? AND u.pseudo = ?

Thanks ;)

1
Try X instead of count in case it's a reserved word issue? - Jeff Breadner
How to do that ? - Tube Yu
select content, count(*) X from hashtags group by content order by X desc limit 10 - Jeff Breadner

1 Answers

0
votes

Looks like the problem is with how you name the count result. Try this:

Tags.findAll({
  group: ['content'],
  attributes: [
    'content',
    [
      sequelize.fn('COUNT', 'content'),
      'count'
    ]
  ],
  order: [['count', 'DESC']],
  limit: 10,
}).then(function(results) {
  // ...
})