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

SunnyBoy's avatar

Query works on local environment but fails on shared hosting???

Not sure what's the issue whit this query? It works just fine in local environment (with mysql v8.0.21) but doesn't work on shared hosting with (mysql 5.6). @snapey Any suggestions????

$query = DB::table('campaigns')
    ->join('categories', 'campaigns.category_id', '=', 'categories.id')
    ->leftJoin('donations', 'campaigns.id', '=', 'donations.campaign_id')
    ->select(
        'campaigns.id',
        'campaigns.title',
        'categories.name',
        'campaigns.status',
        'campaigns.user_id',
        DB::raw('SUM(donations.donation_amount) as donation_amount')
    )
    ->groupBy('campaigns.id');

getting this error not sure why??? Is there something I need to know?

SQLSTATE[42000]: Syntax error or access violation: 1055 'mysql_db.campaigns.title' isn't in GROUP BY (SQL: select `campaigns`.`id`, `campaigns`.`title`, `categories`.`name`, `campaigns`.`status`, `campaigns`.`user_id`, SUM(donations.donation_amount) as donation_amount from `campaigns` inner join `categories` on `campaigns`.`category_id` = `categories`.`id` left join `donations` on `campaigns`.`id` = `donations`.`campaign_id` where `campaigns`.`user_id` = 4 group by `campaigns`.`id`)
0 likes
11 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You cannot do a group by on things that you dont select as well

$query = DB::table('campaigns')
    ->join('categories', 'campaigns.category_id', '=', 'categories.id')
    ->leftJoin('donations', 'campaigns.id', '=', 'donations.campaign_id')
    ->select(
        'campaigns.id',
        'campaigns.title',
        'categories.name',
        'campaigns.status',
        'campaigns.user_id',
        DB::raw('SUM(donations.donation_amount) as donation_amount')
    )
    ->groupBy([
        'campaigns.id',
        'campaigns.title',
        'categories.name',
        'campaigns.status',
        'campaigns.user_id',
]);

or

$query = DB::table('campaigns')
    ->join('categories', 'campaigns.category_id', '=', 'categories.id')
    ->leftJoin('donations', 'campaigns.id', '=', 'donations.campaign_id')
    ->select(
        'campaigns.id',
        DB::raw('SUM(donations.donation_amount) as donation_amount')
    )
->groupBy('campaigns.id');
2 likes
rodrigo.pedra's avatar

Your local MySQL is not eforcing strict group bys and your server is.

When you use GROUP BY you should list all non-aggregate columns:

$query = DB::table('campaigns')
    ->join('categories', 'campaigns.category_id', '=', 'categories.id')
    ->leftJoin('donations', 'campaigns.id', '=', 'donations.campaign_id')
    ->select(
        'campaigns.id',
        'campaigns.title',
        'categories.name',
        'campaigns.status',
        'campaigns.user_id',
        DB::raw('SUM(donations.donation_amount) as donation_amount')
    )
    ->groupBy([
        'campaigns.id',
        'campaigns.title',
        'categories.name',
        'campaigns.status',
        'campaigns.user_id',
    ]);

Or at least, depending on your database config, a primary/unique column from each table that have a non-aggregate column (easier to list all of them)

2 likes
Sinnbeck's avatar

It is possible to turn the strict version off by adding this to your database config

            'strict' => false,

But it isnt recommended

2 likes
rodrigo.pedra's avatar

As @sinnbeck mentioned , it is not recommended to turn strict mode off, it can lead to unexpected and hard to debug results.

I mentioned the strict mode in my response to highlight why it might be working on one installation and not the other.

The adoption of strict mode by MySQL was an evolution to prevent common errors and make it more compatible with other DBMS engines.

2 likes
SunnyBoy's avatar

Ya this works but what you say make complete sense! Thanks

'strict' => false,

Please or to participate in this conversation.