我正在尝试编写这个原始查询
SELECT users.status,count(`users`.`id`) AS `count`
FROM `users` AS `users`
INNER JOIN `organization_entries` AS `organizationEntries` ON `users`.`id` = `organizationEntries`.`user_id`
AND `organizationEntries`.`organization_id` = '1'
AND `organizationEntries`.`type` = '001'
group by users.status;
在 Sequelize 。
我像这样在 Sequelize 中编写了这个查询
db.users
.findAll({
attributes: [
'status',
[db.sequelize.fn('COUNT', 'users.id'), 'entries']
],
include: {
model: db.organizationEntries,
where: {
organization_id: req.params.org_id,
type: '001',
},
},
group: ['users.status']
}).then(c => {
console.log(c);
let active_users = {
active_users: c,
}
return res.send(active_users);
})
.catch(next);
但是当我运行这个查询时,我收到了这个错误
code "ER_WRONG_FIELD_WITH_GROUP" errno 1055 sqlState "42000"
sqlMessage "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ontro.users.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"
我无法理解我做错了什么。
最佳答案
If the ONLY_FULL_GROUP_BY SQL mode is enabled, MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.
请阅读 MySQL Handling of GROUP BY 。
关于node.js - 在 Sequelize 中使用带有内部连接的组给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48724002/