$old_records = DB::connection("mysql_old_s")->table('leads')
->select(DB::raw('count(*) as source_count, source'))
->groupBy('source')
->get();
dd($old_records); // works right
该查询正常运行。我想再添加一列,例如id或created_at进行选择
我无法做到以下几点:
$old_records = DB::connection("mysql_old_s")->table('leads')
->select('id',DB::raw('count(*) as source_count, source'))
->groupBy('source')
->get();
dd($old_records); // it gives an error
它给我一个错误,是:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP
BY clause and contains nonaggregated column 'crmclini_crmv2.leads.id' which is not functionally
dependent on columns in GROUP BY
clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `id`, count(*) as
source_count, source , id from `leads` group by `source`)
您应在group by子句中包括您选择的列,以对它们进行分组:
$old_records = DB::connection("mysql_old_s")->table('leads')
->select('id',DB::raw('count(*) as source_count, source'))
->groupBy(['id','source'])
->get();
第二个选项(我不推荐),您可以在config / database.php中禁用only_full_group_by选项:
'连接'=> [...
'mysql' => [
...
'strict' => false,
...
],
]
除非您有特殊条件,否则请根据您选择的列对查询进行分组
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句