Sequelize 聚合计数函数 returns where 子句的错误值
Sequelize Aggregate count function returns wrong value with where clause
我在 postgresql 中有两个表 - 系统和条件。
所有条件都会有一个 systemID。
所有条件都会有一个字段'publishedStatus'
我的任务是让所有系统都有相关的 'published' 个条件。
下面是我的查询。
const allSystem = await db.System.findAll({
subQuery: false,
attributes: ["id", "title", [db.sequelize.fn("COUNT", db.sequelize.col("conditions.systemID")), "conditionsCount"]],
include: [{
model: db.Condition,
as: "conditions",
attributes: [],
where: { publishedStatus: "published" },
}],
group: ['System.id', 'conditions.systemID'],
});
当我在 'include' 中使用 'where' 子句时 -> 具有 0 'published' 条件的系统将被忽略。
而如果我删除 'where' 子句 -> 包含 0 个条件的系统。但它包括所有 publishedStatus
的条件
参考结果:
Background:
System 1 has 10 conditions. 8 published and 2 unpublished
System 2 has 4 conditions. 0 published and 4 unpublished
System 3 has 0 conditions. 0 published and 0 unpublished
Expected Result:
System 1 = 8 conditions
System 2 = 0 conditions
System 3 = 0 conditions
Without Where Clause:
System 1 = 10 conditions
System 2 = 4 conditions
System 3 = 0 conditions
With Where Clause: (Only 1 row is returned)
System 1 = 8 conditions
如何达到我想要的结果?请帮忙。
您需要 required: false
使用 OUTER JOIN 来包含空条件。
const allSystem = await db.System.findAll({
subQuery: false,
attributes: ["id", "title", [db.sequelize.fn("COUNT", db.sequelize.col("conditions.systemID")), "conditionsCount"]],
include: [{
model: db.Condition,
as: "conditions",
attributes: [],
where: { publishedStatus: "published" },
required: false // <-------------
}],
group: ['System.id', 'conditions.systemID'],
});
我在 postgresql 中有两个表 - 系统和条件。 所有条件都会有一个 systemID。 所有条件都会有一个字段'publishedStatus'
我的任务是让所有系统都有相关的 'published' 个条件。
下面是我的查询。
const allSystem = await db.System.findAll({
subQuery: false,
attributes: ["id", "title", [db.sequelize.fn("COUNT", db.sequelize.col("conditions.systemID")), "conditionsCount"]],
include: [{
model: db.Condition,
as: "conditions",
attributes: [],
where: { publishedStatus: "published" },
}],
group: ['System.id', 'conditions.systemID'],
});
当我在 'include' 中使用 'where' 子句时 -> 具有 0 'published' 条件的系统将被忽略。
而如果我删除 'where' 子句 -> 包含 0 个条件的系统。但它包括所有 publishedStatus
的条件参考结果:
Background:
System 1 has 10 conditions. 8 published and 2 unpublished
System 2 has 4 conditions. 0 published and 4 unpublished
System 3 has 0 conditions. 0 published and 0 unpublished
Expected Result:
System 1 = 8 conditions
System 2 = 0 conditions
System 3 = 0 conditions
Without Where Clause:
System 1 = 10 conditions
System 2 = 4 conditions
System 3 = 0 conditions
With Where Clause: (Only 1 row is returned)
System 1 = 8 conditions
如何达到我想要的结果?请帮忙。
您需要 required: false
使用 OUTER JOIN 来包含空条件。
const allSystem = await db.System.findAll({
subQuery: false,
attributes: ["id", "title", [db.sequelize.fn("COUNT", db.sequelize.col("conditions.systemID")), "conditionsCount"]],
include: [{
model: db.Condition,
as: "conditions",
attributes: [],
where: { publishedStatus: "published" },
required: false // <-------------
}],
group: ['System.id', 'conditions.systemID'],
});