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

noblemfd's avatar

production.ERROR: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY in Ubuntu

This code was working perfectly on my local system,:

     $all_leave_record = HrLeaveRequest::select(DB::raw("COUNT(leave_status) as count"), DB::raw("MONTHNAME(created_at) as month"))
        ->where('company_id', $userCompany)
        ->whereYear('created_at', date('Y'))
        ->orderBy(DB::raw("MONTH(created_at)"), 'ASC')
       ->groupBy(DB::raw("MONTHNAME(created_at)"))
        ->get();
    $all_leave_data = [];

    foreach($all_leave_record  as $row) {
       $all_leave_data['leave_label'][] = $row->month;
       $all_leave_data['all_leave_data'][] = (int) $row->count;
     }
     $all_leave_data['all_leave_chart_data'] = json_encode($all_leave_data); 

        $approved_leave_record = HrLeaveRequest:: select(DB::raw("COUNT(leave_status) as count"), DB::raw("MONTHNAME(created_at) as month"))
             ->where('company_id', $userCompany)
        ->where('leave_status', '=', 4)
        ->whereYear('created_at', date('Y'))
        ->orderBy(DB::raw("MONTH(created_at)"), 'ASC')
       ->groupBy(DB::raw("MONTHNAME(created_at)"))
        ->get();
    //    dd($approved_leave_record);
    $approved_leave_data = [];

    foreach($approved_leave_record  as $row) {
       $approved_leave_data['leave_label'][] = $row->month;
       $approved_leave_data['approved_leave_data'][] = (int) $row->count;
     }
   $approved_leave_data['approved_leave_chart_data'] = json_encode($approved_leave_data); 
   
     //Rejected Leaves          
    $rejected_leave_record = HrLeaveRequest::select(DB::raw("COUNT(leave_status) as count"), DB::raw("MONTHNAME(created_at) as month"))
        ->where('company_id', $userCompany)
        ->where('leave_status', '=', 3)
        ->whereYear('created_at', date('Y'))
        ->orderBy(DB::raw("MONTH(created_at)"), 'ASC')
       ->groupBy(DB::raw("MONTHNAME(created_at)"))
        ->get();
    
    $rejected_leave_data = [];

    foreach($rejected_leave_record  as $row) {
       $rejected_leave_data['leave_label'][] = $row->month;
       $rejected_leave_data['rejected_leave_data'][] = (int) $row->count;
     }
   $rejected_leave_data['rejected_leave_chart_data'] = json_encode($rejected_leave_data); 

but when I deployed to Ubuntu 18 server, I got this error:

production.ERROR: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'hr_leave_requests.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select COUNT(leave_status) as count, MONTHNAME(created_at) as month from hr_leave_requests where company_id = 1 and year(created_at) = 2020 group by MONTHNAME(created_at) order by MONTH(created_at) asc) {"userId":2,"exception":"[object] (Illuminate\Database\QueryException(code: 42000): SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'hr_leave_requests.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select COUNT(leave_status) as count, MONTHNAME(created_at) as month from hr_leave_requests where company_id = 1 and year(created_at) = 2020 group by MONTHNAME(created_at) order by MONTH(created_at) asc) at /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, Doctrine\DBAL\Driver\PDOException(code: 42000): SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'hr_leave_requests.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:63, PDOException(code: 42000): SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'hr_leave_requests.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:61)

How do I get it resolved.

Thank you

0 likes
6 replies
guybrush_threepwood's avatar

Hi,

According to the server configuration (sql_mode=only_full_group_by) you can only order by columns appearing in the GROUP BY clause or aggregates. This is meant to signal logic errors.

The sensible solution would be to modify your query to abide by this rule (sometimes people store the main query inside a subquery so they can order the result by whatever they want).

The workaround would be to disable strict mode in your Laravel database configuration (config/database.php), but it's not recommended:

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

Regards

jlrdw's avatar

Why would you change that permanently when you can change it for just one single query.

config()->set('database.connections.your_connection.strict', false);

// change your_connection to actual connection

// then run your query

The link explains this.

Also study this and get a better understanding of what is actually happening.

https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html

guybrush_threepwood's avatar

I personally wouldn't change it permanently nor for a single query (I wasn't aware that was a possibility).

I saw your message when I finished composing and posting my reply (it wasn't there when I started typing).

jlrdw's avatar

That'sounds okay, I agree I would look at reworking the query first but if it works fine at least there's a way to use it. Many of these type of queries worked in older versions of MySQL.

I am sure many developers in older applications have code that did not have such a restriction I myself did and that's why I discovered the above, and figured I would share it with others.

1 like
trungtranqn91's avatar

Could you dump query and run it directly in DB prod for test? I thought the issue cause the data different from local and prod. And you can not group the data when it have null data. Just my opinion

Please or to participate in this conversation.