Sequelize with join, group by, having, order by

前端 未结 2 485
猫巷女王i
猫巷女王i 2021-01-20 11:17

How this query can be written using ORM?

SELECT p.id, 
    p.name, 
    COUNT(c.id) counter 
FROM Posts p 
LEFT JOIN Comments c 
    ON c.post_id = p.id 
WHE         


        
2条回答
  •  时光取名叫无心
    2021-01-20 12:07

    Try this:

    Post.findAll({
      where: {rating: {gt: 100}},
      include: [Comments],
      having: ['COUNT(?) >= ?', '`comment`.`id`', 10]
    })
    

    Read my research by this question.

    i use sequelize@2.0.0-dev9

提交回复
热议问题