mardi 28 mai 2019

Query relation and group by in query builder

Getting a sql error in my query, i believe maybe is malformed my query builder but cant figure out what im doing wrong.

Alredy tried testing my query directlly in the DB and it works, but when translating with query builder it doesnt work.

$totalUserForCountry = DB::table('countries')
    ->select('countries.name', DB::raw('COUNT(users.country_id) as total'))
    ->join('users', 'users.country_id', '=', 'countries.id')
    ->groupBy('users.country_id')
    ->get();

My error code is:

SQLSTATE[42000]: Syntax error or access violation: 1055 'db.countries.name' isn't in GROUP BY (SQL: select countries.name, COUNT(users.country_id) as total from countries
inner join users on users.country_id = countries.id group by users.country_id)



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2wohU6b
via IFTTT

Aucun commentaire:

Enregistrer un commentaire