简体   繁体   中英

How to make a non-primary column unique in a returned result-set with Laravel's eloquent?

In this example below I am requesting all colleges where the state is CO. But I'd like to say groupBy('name') or something in order to get a single college name record.

I get the following error when I attempt:

# 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);

I don't particularly care which college (id) is returned, I just want to be able to return one of each college name per state.

Any assistance is appreciated.

归还国家

尝试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM