在下面的示例中,我要求所有州都是CO的大学。但我想说groupBy('name')
或某些以获得单一的大学名称记录。
尝试时出现以下错误:
# Error:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'imr.colleges.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from `colleges` where `state` = CO group by `name` limit 15 offset 135)
# Code:
College::where('state',$state)->groupBy('name')->paginate(15);
我并不特别关心返回哪所大学(id),我只想能够返回每个州的每个大学名称中的一个。
感谢任何帮助。
答案 0 :(得分:2)
试试这个
College::selectRaw('GROUP_CONCAT(id), name, GROUP_CONCAT(state)')->where('state',$state)->groupBy('name')->paginate(15);