【问题标题】:Using group with Inner Join in Sequelize giving error在 Sequelize 中使用带有 Inner Join 的组给出错误
【发布时间】:2018-02-11 02:27:51
【问题描述】:

我正在尝试编写这个原始查询

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 中这样写了这个查询

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);

但是当我运行这个查询时,我得到了这个错误

代码 "ER_WRONG_FIELD_WITH_GROUP" errno 1055 sqlState "42000"

sqlMessage "SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且 包含非聚合列“ontro.users.id”,它不是 功能上依赖于 GROUP BY 子句中的列;这是 不兼容 sql_mode=only_full_group_by"

我无法理解我做错了什么。

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    如果启用了 ONLY_FULL_GROUP_BY SQL 模式,MySQL 拒绝选择列表 HAVING 的查询 条件或 ORDER BY 列表指的是非聚合列 既不在 GROUP BY 子句中命名,也不在功能上依赖于 他们。

    请继续阅读MySQL Handling of GROUP BY

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      相关资源
      最近更新 更多