简体   繁体   中英

GroupBy in Laravel 5.3 and PHP 7 not working

Group by is problematic since I switched to PHP 7 and having aggregate values doesn't apply to the way I want to use it.

I've got a list and sometimes there are repeats in the list. For example:

Song Title A Song Title B Song Title A (reprise) Song Title C

In the results, Song Title A should only show up once. Previously, I had done this with group by, where the ID that determines the song title is grouped.

My table structure is:

id
group_id
song_title_id

Song_title_id relates to:

id
song_title

In the first table, there can be multiple instances of the same song_title_id. And when results are returned, I want there to only one instance of each song_title_id in the results.

Previously, I'd have done:

DB::table('group_songs')
         ->groupBy('song_title_id')
         ->get();

Since updating to PHP 7, I get an error:

Syntax error or access violation: 1055 '[table/column name]' isn't in GROUP BY

What am I doing wrong?

'mysql' => [
    'strict' => false,
],

By How to resolve "isn't in GROUP BY" error in mysql query Each non-aggregated field should be grouped, so try

DB::table('group_songs')
         ->groupBy('song_title_id')
         ->groupBy('group_id')
         ->groupBy('id')
          ->get();

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