使用内连接计数对查询进行续集 [英] Sequelize query with count in inner join
本文介绍了使用内连接计数对查询进行续集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将此查询转换为对查询对象进行续集的正确方法是什么?
I am trying to convert this query to sequelize query object what is the right wayto do it?
SELECT families.id, count('answers.familyId') FROM families LEFT JOIN
answers on families.id = answers.familyId WHERE answers.isActive=1 AND
answers.answer=1 GROUP BY families.id HAVING COUNT('answers.familyId')>=6
推荐答案
假设 Family
是你的 families
sequelize 模型,而 Answer
是您的 answers
续集模型,而 sequelize
是您的 Sequelize 实例
Let's assume that Family
is your families
sequelize model and Answer
is your answers
sequelize model, and sequelize
is your Sequelize instance
Family.findAll({
attributes: ['*', sequelize.fn('COUNT', sequelize.col('Answers.familyId'))],
include: [
{
model: Answer,
attributes: [],
where: {
isActive: 1,
answer: 1
}
}
],
group: '"Family.id"',
having: sequelize.where(sequelize.fn('COUNT', sequelize.col('Answers.familyId')), '>=', 6)
}).then((families) => {
// result
});
有用的文档链接:
这篇关于使用内连接计数对查询进行续集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文