在Laravel中选择和分组

时间:2017-08-30 03:15:23

标签: php mysql laravel laravel-5 laravel-5.3

如何在laravel控制器中使用此脚本?

 SELECT MAX(branch_tbl.branch_name) AS Branch_Name, COUNT(staffinfo.branchID) AS Count_Branch
    FROM branch_tbl
    LEFT JOIN staffinfo ON staffinfo.branchID = branch_tbl.branch_id
    GROUP BY branch_tbl.branch_id;

1 个答案:

答案 0 :(得分:2)

你可以使用它

DB::table('branch_tbl')->select(DB::raw('MAX(branch_tbl.branch_name) AS Branch_Name, COUNT(branch_tbl.branch_id) AS Count_Branch'))
    ->leftJoin('staffinfo', 'staffinfo.branchID', '=', 'branch_tbl.branch_id')
    ->groupBy('branch_tbl.branch_id')->get()