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

appyapp's avatar

Query runs ok in phpmyadmin but DB::select() throws exception

I have this weird issue - this query below in one of my controller method is throwing me an exception error.

$sql = "SELECT acd.*, DATE_FORMAT(acd.cdate, '%d/%m/%Y') cdate_disp,
  GROUP_CONCAT(CONCAT_WS(', ', car.type, car.session, crd.name) SEPARATOR '<br />') rd_names,
  acb.booking_status my_booking_status
FROM app_data acd
INNER JOIN crmaccounts ca ON ca.id = acd.client_crm_id
LEFT JOIN crmaccount_rdids car ON car.account_id = ca.id
LEFT JOIN crmrd_ids crd ON crd.id = car.rd_id
LEFT JOIN app_bookings acb ON acb.call_ref_id = acd.call_ref AND acb.user_id = 12391
WHERE 1=1 
AND acd.client_crm_id NOT IN (select account_id from bannedstaff WHERE user_id=12391)
GROUP BY acd.id
ORDER BY acd.cdate, ctiming";

DB:select($sql);

The exception is

 Illuminate \ Database \ QueryException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY (SQL: SELECT acd.*, DATE_FORMAT(acd.cdate, '%d/%m/%Y') cdate_disp, GROUP_CONCAT(CONCAT_WS(', ', car.type, car.session, crd.name) SEPARATOR '<br />') rd_names, acb.booking_status my_booking_status FROM app_data acd INNER JOIN crmaccounts ca ON ca.id = acd.client_crm_id LEFT JOIN crmaccount_rdids car ON car.account_id = ca.id LEFT JOIN crmrd_ids crd ON crd.id = car.rd_id LEFT JOIN app_bookings acb ON acb.call_ref_id = acd.call_ref AND acb.user_id = 12391 WHERE 1=1 AND acd.client_crm_id NOT IN (select account_id from bannedstaff WHERE user_id=12391) GROUP BY acd.id ORDER BY acd.cdate, ctiming)
Previous exceptions

    SQLSTATE[42000]: Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY (42000)

However, if I go in PhpMyAdmin and copy paste the above query - it gives me the out put without any issues.

What's going wrong here? Please take a look for me.

0 likes
12 replies
appyapp's avatar

I think this is what is happening.

I have acd.* in the select and GROUP BY acd.id in the group by. The error message above says Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY so I changed GROUP BY acd.id to GROUP BY acd.id, acd.call_ref and refreshed. Then it threw same exception again but this time on different column. So in my database table acd.id is first column, acd.call_ref is second column - after I put acd.call_ref in the group by, the exception is showing now 3rd column in the exception error. If I put this 3rd one in the group by I would show error for the 4th one and so on.

Why is it doing that?

grenadecx's avatar

I had a similar problem a way back, same error but it was a difference between how mysql and mariadb handled the same query, where mysql was fine but mariadb required columns from the table to be put inside the group by.

But since you have the problem with the same sql instance, I'm not sure if it's the same problem.

If you add acd.call_ref to the group by as well, do you get a different error?

For example, I had a query that looked like

        $payload['profile'] = Profiles::with([
            'published' => function($query){
                $query->with('files');
                $query->withCount('files');
                $query->with('data.data_array');
            },
            'files',
        ])->whereHas('current.services.callgroups', function($query) use ($payload){ // Fetch based on callgroups
            $query->whereIn('callgroup_id', $payload['callgroups'] ?? []);
        })
            ->whereHas('published')
            ->LeftJoin('viatel_profiles', function ($join) { // join viatel_profiles with profiles table based on id
                $join->on('profiles.published_id', '=', 'viatel_profiles.version_id');
            })
            ->groupBy('profiles.id', 'profiles.published_id', 'profiles.current_id', 'profiles.type_id', 'profiles.user_id', 'profiles.name', 'profiles.image_id', 'profiles.is_active', 'profiles.delete_requested', 'profiles.created_at', 'profiles.updated_at') // Group by profile_id to prevent duplicates
            ->select([
                'profiles.*',
                DB::raw('SUM(viatel_profiles.online_status) as viatel_profiles_online'), 
                DB::raw('COUNT(viatel_profiles.online_status) as viatel_profiles_total'), 
                DB::raw('(CASE WHEN COUNT(viatel_profiles.online_status) = SUM(viatel_profiles.online_status) THEN 1 ELSE 0 END) AS viatel_online_status') // Magic
            ])
            ->findOrFail($profile_id);

As you can see in my example, I had to add all the profiles columns inside the group by for it to give the same results as mysql only required the table.id.

Can't really say why it does it.

appyapp's avatar

@GRENADECX - Yes please see my reply above as i found out that it seems to be asking for all columns to be put in the group by.

I am on my local system which has Maria DB. I am connecting to the same database via both laravel and phpmyadmin.

If I copy paste the query in PhpMyAdmin it works fine without any issues. However, if I run via Laravel, I get the above.

munazzil's avatar

Just use a \ before the DB as like below,

\DB:select($sql);
appyapp's avatar

@MUNAZZIL - No - that's no an issue as I have use Illuminate\Support\Facades\DB; at the top.

grenadecx's avatar

@APPYAPP - Sorry, I'm not sure what causes this.

For me, I didn't have time to solve the issue when I was working on that project, so I just solved it as is and moved on. If you do manage to figure out why, I'm interested in the solution as well.

munazzil's avatar

Have you used below one,if error occur mention over here,

    DB::raw($sql);
appyapp's avatar

@MUNAZZIL - @grenadecx and @munazzil

Yes I tried all these below.

DB::select($sql);
 
DB::raw($sql);

DB::select(DB::raw($sql));

//even with pdo 
$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(\PDO::FETCH_OBJ);

 PDOException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 'mydatabase.acd.call_ref' isn't in GROUP BY
    

None of these work. :(

Show stopper for me to be honest.

appyapp's avatar
appyapp
OP
Best Answer
Level 2

@GRENADECX - I have found one solution so far to get that working.

If you disable strict mode in config/database.php inside connections under mysql - I can get that query working without having to add all the column names in the group by.

Need to understand if I disable strict mode what other issues could come up?

munazzil's avatar

Instead of GroupBy use UNION or UNION ALL and check.

Please or to participate in this conversation.