【发布时间】: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