mardi 21 juin 2016

Laravel 5.1: Query Builder Select Multi table and SUM result

My code

DB::table('users_tb')->leftjoin('device_tb', 'device_tb.user_id', '=', 'users_tb.user_id')
->leftjoin('part_tb', 'device_tb.device_id', '=', 'part_tb.device_id')
->select('users_tb.user_name', DB::raw("(SELECT COUNT(part_tb.id) FROM part_tb) AS counter"))
->where('users_tb.level', '>', 7)
->havingRaw("(SELECT COUNT(part_tb.id) FROM part_tb) > 0")
->groupBy('device_tb.device_id')
->orderBy('users_tb.user_name')
->get()->toArray();

My result:

Name1 - 3
Name1 - 4
Name1 - 3
Name2 - 11
Name2 - 2

How i can SUM counter? Example:

Name1 - 10 (3+4+3)
Name2 - 13 (11 + 2)

Somebody help me, please!!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire