Level 6
You maybe find an approach using case when
SELECT
COUNT(id) as total,
SUM(CASE WHEN age IN (8, 9, 10) THEN 1 ELSE 0 END) as youngerTenCount,
SUM(CASE WHEN age NOT IN(8, 9, 10) THEN 1 ELSE 0 END) as olderTenCount
FROM students
So you basically will have three DB::raws in your select
Student::select([
DB::raw("COUNT(id) as total"),
DB::raw("SUM(CASE WHEN age IN (8, 9, 10) THEN 1 ELSE 0 END) as youngerTenCount"),
DB::raw("SUM(CASE WHEN age NOT IN(8, 9, 10) THEN 1 ELSE 0 END) as olderTenCount")
]);
Not tested.
Hopefully understood you correctly.
1 like