在 api 平台响应中包含计算出的 SUM 列

问题描述 投票:0回答:1

我使用 SF 6.3 和最新的 api 平台。

我正在尝试对公司所有职位的所有可用职位进行求和。

$queryBuilder
        ->leftJoin('company.jobs', 'job')
        ->addSelect('SUM(job.positions) as jobPositionCount')
        ->addGroupBy('company.id');

这个抛出了一个错误

An exception occurred while executing a query: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #7 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hira_db.o0_.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

但是生成的 SQL 查询在 MySQL Workbench 上运行良好

SELECT 
    c0_.id AS id_0,
    c0_.name AS name_1,
    c0_.is_active AS is_active_2,
    c0_.created_at AS created_at_3,
    c0_.updated_at AS updated_at_4,
    SUM(o1_.positions) AS sclr_5
FROM company c0_
LEFT JOIN opening o1_ ON c0_.id = o1_.company_id AND (o1_.deleted_at IS NULL)
GROUP BY c0_.id

我在教义部分错过了什么?

sql mysql symfony doctrine api-platform.com
1个回答
0
投票

当 MySQL 设置 ONLY_FULL_GROUP_BY 为 ON(应该如此)时,如果不指定所有(必要的*)非聚合列,则无法使用 group by 查询。

SELECT 
      c0_.id AS id_0
    , c0_.name AS name_1
    , c0_.is_active AS is_active_2
    , c0_.created_at AS created_at_3
    , c0_.updated_at AS updated_at_4
    , SUM(o1_.positions) AS sclr_5
FROM company c0_
LEFT JOIN opening o1_ ON c0_.id = o1_.company_id AND (o1_.deleted_at IS NULL)
GROUP BY
      c0_.id
    , c0_.name
    , c0_.is_active
    , c0_.created_at
    , c0_.updated_at

非聚合列是 select 子句中不涉及聚合的任何列(sum、count、min、max 等)。唯一的例外是,您可以省略“功能上依赖于”其他非聚合列之一的任何非聚合列。

© www.soinside.com 2019 - 2024. All rights reserved.