4
votes

I've become extremely confused I'm trying to run a query through laravels query builder which should work but it's throwing odd errors.

I'm trying to count records from the tips table where the status is 'Won' between a daterange and then join the users table on the user_id to retrieve the users username.

This is my query builder

    $topfiveTipsters = DB::table('tips')
      ->select(DB::raw('count(status) as wincount, users.name'))
      ->join('users', 'users.id', '=', 'tips.user_id')
      ->whereBetween('tips.created_at',[$start,$end])
      ->where('status','Won')
      ->groupBy('users.id')
      ->orderBy('wincount', 'desc')
      ->get();

However it's throwing the error

QLSTATE[42000]: Syntax error or access violation: 1055 'digthetip.users.name' isn't in GROUP BY (SQL: select count(status) as wincount, users.name from tips inner join users on users.id = tips.user_id where tips.created_at between 2016-11-01 00:00:00 and 2016-11-02 10:39:02 and status = Won group by users.id order by wincount desc)

but if I run the exact query the error has outputted into the SQL console within PHPMyAdmin the query runs fine and returns the results I require.

Am I missing something? I'm relatively new to Laravel and I'm massively confused how it's still throwing an error.

1
Can you post both SQL query's that you are comparing please?Umbert P.
You can use addSelect laravel.com/docs/5.3/queries#selects after joinVahe Galstyan
The query I'm attempting to run is this select count(status) as wincount, users.name from tips inner join users on users.id = tips.user_id where tips.created_at between '2016-11-01 00:00:00' and '2016-11-02 10:39:02' and status = 'Won' group by users.id order by wincount descParadigm
What happens if you add the users.id in the select?Umbert P.

1 Answers

4
votes

Ok so it turns out you can turn of SQL strict mode in /app/database.php which will prevent the error from happening.

'strict' => false,