1

I have this Eloquent query:

return DB::table('users')
        ->leftJoin('bet', function($join) use ($competition_id){
          $join->on('users.id', '=', 'bet.user_id');
          $join->on('bet.competition_id','=', DB::raw("$competition_id"));
        })
        ->select('users.*', DB::raw('SUM(points) as score'))->get();

it returns me an error:

SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select users.*, SUM(points) as score from users left join bet on users.id = bet.user_id and bet.competition_id = 1)

if I just copy-paste the generated SQL (in bold) in PhpMyAdmin, it works perfectly...

Could you please help me solving this issue? because it makes no sense to me...

Thank you

SOLUTION

The problem was just the missing group by:

return DB::table('users')
    ->leftJoin('bet', function($join) use ($competition_id){
      $join->on('users.id', '=', 'bet.user_id');
      $join->on('bet.competition_id','=', DB::raw("$competition_id"));
    })
    ->select('users.*', DB::raw('SUM(points) as score'))->groupBy('users.id')->get();

SOLUTION #2

Depending on the version or configuration of MySQL, the solution above is not working and I had to groupBy all the fields as advised by Eric:

return DB::table('users')
    ->leftJoin('bet', function ($join) use ($competition_id) {
        $join->on('users.id', '=', 'bet.user_id');
        $join->on('bet.competition_id', '=', DB::raw("$competition_id"));
    })
    ->select('users.*', DB::raw('SUM(points) as score'))
    ->groupBy('users.id')
    ->groupBy('users.name')
    ->groupBy('users.firstName')
    ->groupBy('users.lastName')
    ->groupBy('users.email')
    ->groupBy('users.admin')
    ->groupBy('users.password')
    ->groupBy('users.remember_token')
    ->groupBy('users.created_at')
    ->groupBy('users.updated_at')->get();
}
4
  • mysql I assume? Commented Mar 8, 2018 at 21:04
  • Your laravel is probably using strict mode, while the servers default isn't. You probably need to add ->groupBy('users.id). Commented Mar 8, 2018 at 21:06
  • You have SUM(), but I don't see any GROUP BY in your statement. Commented Mar 8, 2018 at 21:13
  • Put all non-aggregated columns in GROUP BY. Your query won't even run in all dbms, except for maybe MySQL. Commented Mar 8, 2018 at 21:14

1 Answer 1

2

I have many questions but 4. is the cause of it

  1. Why aren't you using the ORM? You should take advantage of the framework whenever possible
  2. Why DB::raw on $competition_id? it's just a value you're inserting into the query
  3. You can use selectRaw on your final select to avoid another DB::raw
  4. It is most likely the lack of a groupBy (your sum is at fault). You can do this by placing a group by or disabling strict mode on MySql

Try this:

return DB::table('users')
        ->leftJoin('bet', function($join) use ($competition_id){
          $join->on('users.id', '=', 'bet.user_id');
          $join->on('bet.competition_id','=', DB::raw("$competition_id"));
        })
        ->select('users.*', DB::raw('SUM(points) as score'))->groupBy('users.id')->get();
Sign up to request clarification or add additional context in comments.

3 Comments

I use $competition_id in a raw request otherwise it seems to be considered as a column: (unknown column '1'). Your query is almost good, I just had to change a bit the select part to make it work (see my updated post). Thanks!
Glad you got it to work! If you disable strict mode, you won't need groupBy (although you should). This is because: If you are going to sum a column, mysql needs to know what is it based on :) Feel free to classify this answer as a solution or provide the solution for future users
What is funny with this request is that depending on which computer i'm developing my app, it is still not working as is. I think it's related to the version and/or configuration of MySQL. See my updated answer to have the other solution. Thanks again!

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.