Sequelize with join, group by, having, order by [英] Sequelize with join, group by, having, order by

查看:16
本文介绍了Sequelize with join, group by, having, order by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 ORM 编写此查询?

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 
WHERE p.rating > 100 
GROUP BY p.id
HAVING counter > 10
ORDER BY counter DESC 
LIMIT 5

推荐答案

试试这个:

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

通过这个问题阅读我的研究.

我使用 sequelize@2.0.0-dev9

这篇关于Sequelize with join, group by, having, order by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆