Laravel is written in group after using the where statement.

there are three fields (: score (score) and userid,subject (discipline) in the data table tbscore. The effect I want to achieve is:

select sum(score),subject from (select score,subject from tbscore where userid in(1,2,3)) group by subject

that is, you want to filter out the result set first, and then group by, the result set. How to write

in laravel?
Feb.28,2021

try this

$row = DB::table('tbscore')
    ->select(DB::raw('count(score) as score, subject'))
    ->whereIn('userid', [1,2,3])
    ->group ('subject')
    ->get();

Database-- query Builder

Menu