如何使用Laravel滔滔不绝的声明在返回的结果集中使非主列唯一?

时间:2018-05-14 15:44:10

标签: mysql laravel eloquent

在下面的示例中,我要求所有州都是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),我只想能够返回每个州的每个大学名称中的一个。

感谢任何帮助。

Collection of states returned

1 个答案:

答案 0 :(得分:2)

试试这个

College::selectRaw('GROUP_CONCAT(id), name, GROUP_CONCAT(state)')->where('state',$state)->groupBy('name')->paginate(15);