如何将SQL查询转换为查询生成器

时间:2018-09-24 09:49:58

标签: mysql laravel eloquent

如何将以下查询转换为laravel雄辩的查询?

SELECT demand_costings.pr_number as Demand_Costing,work_types.work_types,
                 SUM(demand_costings.pr_quantity_in_pcs) as Total  
FROM demand_costings
     INNER JOIN work_types
ON demand_costings.worktype_id=work_types.id
GROUP BY work_types.work_types;

2 个答案:

答案 0 :(得分:0)

您可以使用laravel的DB门面:

$data = DB::table('table_name')
    ->select(DB::raw('emand_costings.pr_number as Demand_Costing,work_types.work_types,SUM(demand_costings.pr_quantity_in_pcs) as Total FROM demand_costings INNER JOIN work_types ON demand_costings.worktype_id=work_types.id'))
    ->groupBy('work_types')
    ->get();

答案 1 :(得分:0)

尝试一下:

DB::table('demand_costings')
                ->select([
                    'pr_number as Demand_Costing',
                    'work_types',
                    'SUM(pr_quantity_in_pcs) as Total'
                    ])
                ->join('work_types', 'demand_costings.worktype_id', '=', 'work_types.id')
                ->groupBy('work_types.work_types')
                ->get();