【问题标题】:Laravel 5.3 query builder access violation on group byLaravel 5.3 查询生成器对 group by 的访问冲突
【发布时间】:2017-04-24 18:51:52
【问题描述】:

我已经使用 Laravel 5 一周了,现在我想使用 Query Builder 转换所有现有的原始 SQL 查询,但是我遇到了一个问题。

当我运行以下查询时,我收到此错误消息

SQLSTATE[42000]:语法错误或访问冲突:1055 'procurement.pp_proposals.title' 不在 GROUP BY 中

$proposals = DB::table('tableA')
                ->join('tableB', 'tableA.id', '=', 'tableB.id')
                ->leftJoin('tableC', 'tableA.id', '=', 'tableC.id')
                ->select(DB::raw('tableA.id, title, date_created, date_completed, percent_complete, complete, COALESCE(COUNT(tableC.id), 0) AS total_ids'))
                ->where([
                    ['tableB.user', '=', Auth::user()->username],
                    ['submitted', '=', '0'],
                ])
                ->groupBy('tableA.id')
                ->orderBy('title', 'asc')
                ->get();

这是我的原始 SQL,它工作得非常好,所以我不明白为什么我需要对所有额外的列进行 GROUP BY

SELECT tableA.id, 
        title,
        date_created,
        date_completed,
        percent_complete,
        complete,
        COALESCE(COUNT(tableC.id), 0) AS 'total_ids'
FROM tableA
    INNER JOIN tableB
        ON tableA.id = tableB.id
    LEFT JOIN tableC
        ON tableA.id = tableC.id
WHERE submitted = '0' AND tableB.user = 'user' 
GROUP BY tableA.id
ORDER by title ASC

【问题讨论】:

    标签: mysql sql laravel-5.3 laravel-query-builder


    【解决方案1】:

    我不确定它是如何工作的,但您可以通过将 config/database.php 文件中的 strict = false 设置为您的 MySQL 配置来解决该问题!

    我遇到了同样的问题并将其设置为 false 解决了我的问题!

    其实我只是在这里打开一个问题来找出它发生的原因!

    如果你想关注:Strict database config Laravel 5.2 to Laravel 5.3

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 1970-01-01
      • 2017-10-26
      • 2022-01-21
      • 1970-01-01
      • 2016-12-31
      • 2016-11-16
      • 2019-04-18
      • 2017-12-04
      相关资源
      最近更新 更多