【问题标题】:Laravel groupBy doesn't work properly with PostgreSQLLaravel groupBy 不能与 PostgreSQL 一起正常工作
【发布时间】:2018-04-05 04:17:54
【问题描述】:

没有groupBy 的行数相同。

$authors = AuthorMask::where('company_id', $user->company_id)
        ->groupBy('id', 'author_id')
        ->orderBy('created_at', 'desc')
        ->paginate(5);

如果我使用这个:

$authors = AuthorMask::where('company_id', $user->company_id)
        ->groupBy('author_id')
        ->orderBy('created_at', 'desc')
        ->paginate(5);

我收到一个错误。

SQLSTATE[42803]:分组错误:7 错误:列“author_masks.id” 必须出现在 GROUP BY 子句中或在聚合函数中使用 LINE 1: select * from "author_masks" where "company_id" = $1 group b... ^ (SQL: select * from "author_masks" where "company_id" = 1 group by "author_id" order by "created_at" desc limit 5 offset 0)

【问题讨论】:

    标签: php sql postgresql laravel


    【解决方案1】:

    不幸的是,无法使用 PostgreSQL 运行第二个查询,因为它要求 SELECT 列表中指定的所有字段都属于 GROUP BY 表达式。

    您可以查看here

    【讨论】:

    • 谢谢。但是为什么第一个查询不起作用?不返回错误,只是没有任何变化(例如,我有 3 条 author_id = 1 的记录,但它返回 3 条记录而不是 1 条)。
    猜你喜欢
    • 2021-08-18
    • 2018-11-10
    • 2014-07-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多