gpt4 book ai didi

node.js - 使用 join、group by、having、order by 进行 Sequelize

转载 作者:搜寻专家 更新时间:2023-11-01 00:45:08 24 4
gpt4 key购买 nike

如何使用 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]
})

阅读my research通过这个问题。

我使用 sequelize@2.0.0-dev9

关于node.js - 使用 join、group by、having、order by 进行 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21057302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com