0

i have problem with GroupBy in Laravel. Error that is writen is:

SQLSTATE[42000]: Syntax error or access violation: 1055 'smf.serije_epizode.id' isn't in GROUP BY (SQL: select * from `serije_epizode` where `id_serije` = 1 group by `sezona` order by `sezona` asc)

Also, fields in database are: https://i.sstatic.net/e6akE.png

And query in laravel framework is:

public function prikaz($id)
{
    $serija = DB::table('serije')
        ->where('id', $id)
        ->first();
    $epizode = DB::table('serije_epizode')
        ->where('id_serije', '=', $id)
        ->orderBy('sezona', 'asc')
        ->groupBy('sezona')
        ->get();
    return view('serije.prikaz', ['serija' => $serija, 'epizode' => $epizode]);
}

View blade:

     @foreach ($epizode as $serijaa)
Sezona: {{$serijaa->sezona}} | Epizoda: {{$serijaa->br_epizode}} - {{$serijaa->naziv}
  @endforeach

So why i just can't group by "sezona" (mean Seasons), when i have that. What am i doing wrong. Thanks. :)

1
  • every column in the select list that is not a calculated column must be declared in the group by clause. Commented Nov 25, 2019 at 23:10

2 Answers 2

1

You request for all columns and your columns are not processed (or calculated) then you have to add them into your group by clause.

Sign up to request clarification or add additional context in comments.

Comments

0

There are multiple ways to solve this issue.

The 1st and the 'best practise' is to include you column in the select query.

DB::table('serije_epizode')
        ->select('id_serije')
        ->where('id_serije', '=', $id)
        ->orderBy('sezona', 'asc')
        ->groupBy('sezona')
        ->get();

Inside the select you must have your groupBy column or generally as a rule whatever i inside select should be inside groupBy

Group by can accept multiple values so you can have something like this in your select:

select('serije_epizode','sezona','id_serije')

And in your group by you should have the same;

groupBy('serije_epizode','sezona','id_serije')

The 2nd and the "not so good practise" but have seen it in many projects is inside your database.php file you are going to find your mysql setup.

In there you are going to find a field named strict which has a value true. If you change it to false and run php artisan config:cache your group by will work.

I would strongly suggest to go for the 1st option or even better create a model and use Eloquent but that's up to you.

Another option for you might be to use distinct() or unique() to return unique values from a column instead of groupBy

9 Comments

Thanks for this advice, but still it doesn't work. I'm getting error: Syntax error or access violation: 1055 'smf.serije_epizode.id_serije' isn't in GROUP BY (SQL: select id_serije, sezona, br_epizode from serije_epizode where id_serije = 1 group by sezona order by sezona asc, br_epizode asc)
Judging by your error you did not use my answer. You still have 3 columns in select and 1 in group by.
When i add id_serije in groupby, still getting same error. Syntax error or access violation: 1055 'smf.serije_epizode.br_epizode' isn't in GROUP BY (SQL: select id_serije, sezona, br_epizode from serije_epizode where id_serije = 1 group by id_serije, sezona order by sezona asc, br_epizode asc)
When i add all 3 field in GroupBY, then i have same results as without groupBy. I have tv show that is (id_serije / id_show), and then number season and number episode. I want to find tv show by id then all epizodes group by seasons. That is result that i cant find.
I have more than 1 solutions in my answer :) give a try to the next one if you cant have this work for you. I would leave the 2nd as last though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.