Node.js Sequelize count fn add where子句
下面的代码运行良好,但我只想计算在场的学生人数。我该怎么做呢Node.js Sequelize count fn add where子句,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,下面的代码运行良好,但我只想计算在场的学生人数。我该怎么做呢 Classroom.findAll({ attributes:{ include : [ [sequelize.fn('COUNT', sequelize.col('student.id')), 'count'] // here. where student.present = true; ], }, include: [{ attributes:
Classroom.findAll({
attributes:{
include : [
[sequelize.fn('COUNT', sequelize.col('student.id')), 'count'] // here. where student.present = true;
],
},
include: [{ attributes: [], model: Student, as : 'student', }],
group: [sequelize.col('Classroom.id')]
})
您可以在包含模型中添加该条件:
Classroom.findAll({
attributes:{
include : [
[sequelize.fn('COUNT', sequelize.col('student.id')), 'count']
],
},
include: [
{
attributes: [],
model: Student,
as : 'student',
where : { present : true } // <--------- HERE
}
],
group: [sequelize.col('Classroom.id')]
})
school.findAll({
属性:{
包括:[
[sequelize.fn('COUNT',sequelize.col('student.id'),'COUNT']
],
},
包括:[
{
属性:[],
模特:学生,
作为‘学生’,
其中:{present:true}//如果我还需要学生总数('presentCount'和'totalCount')。那么您可以尝试group:[sequelize.col('classification.id')、sequelize.col('student.present')]
并删除where条件,这可能会引发错误,为此,您必须在include students中添加属性model@farhanlatheef,很抱歉,我不能,因为我不知道它会抛出什么错误,然后它将成为试错,您将发布每个错误,我将逐一解决,我无法测试您的场景,因为我没有数据库或平台美国东部时间。