Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TimiAde's avatar

incompatible with sql_mode=only_full_group_by

I have a query

 $schedules = DB::table('schedules')
                    ->join('events', 'schedules.event_id', '=', 'events.id')
                    ->join('users', 'schedules.user_id', '=', 'users.id')
                    // ->join('predictions', 'users.id', '=', 'predictions.user_id')
                    ->where('schedules.user_id', '=', Auth::id())
                    ->select(DB::raw('DATE(schedules.created_at) as date'), 'schedules.link')
                    ->groupBy('date')
                    ->get();
        return $schedules;

I am getting error

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'kalaadardb.schedules.link' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
SELECT DATE(schedules.created_at) AS date, `link` FROM `schedules` INNER JOIN `events` ON `schedules`.`event_id` = `events`.`id` INNER JOIN `users` ON `schedules`.`user_id` = `users`.`id` WHERE `schedules`.`user_id` = 3 GROUP BY `date`
0 likes
1 reply
LaryAI's avatar
Level 58

The error message is due to the sql_mode=only_full_group_by setting in MySQL. This setting requires that all columns in the SELECT clause must either be in the GROUP BY clause or be aggregated. In this case, the link column is not in the GROUP BY clause and is not aggregated.

To fix this error, you can either add the link column to the GROUP BY clause or use an aggregate function like MAX or MIN to select a single value for the link column. Here's an example of using MAX:

$schedules = DB::table('schedules')
    ->join('events', 'schedules.event_id', '=', 'events.id')
    ->join('users', 'schedules.user_id', '=', 'users.id')
    ->where('schedules.user_id', '=', Auth::id())
    ->select(DB::raw('DATE(schedules.created_at) as date'), DB::raw('MAX(schedules.link) as link'))
    ->groupBy('date')
    ->get();
return $schedules;

In this example, we're using MAX to select the maximum value of the link column for each group. This satisfies the requirement of the only_full_group_by setting.

1 like

Please or to participate in this conversation.