由于不在GROUP BY中,Laravel 5.4 Group By无法正常工作

时间:2018-01-29 01:00:03

标签: laravel laravel-5.4

当我使用我的代码时,它正常工作

 $pick_list_items = DB::table('pick_list_detail')
    ->select(['pick_list_detail.*','sale_invoice_detail.product_id','sale_invoice_detail.product_qty','sale_invoice_detail.uom','sale_invoice_detail.uom_factor'])
    ->where('pick_list_detail.pick_list_id',$id)
    ->join('sale_invoices','pick_list_detail.sale_invoice_id','=','sale_invoices.id')
    ->join('sale_invoice_detail','sale_invoice_detail.sale_invoice_id','=','pick_list_detail.sale_invoice_id')
    ->get();

我得到了我需要的输出,所以它是这样的,许多sale_invoices中有很多产品,我的查询需要根据product_id对产品进行分组,所以我做的是:

 $pick_list_items = DB::table('pick_list_detail')
    ->select(['pick_list_detail.*','sale_invoice_detail.product_id','sale_invoice_detail.product_qty','sale_invoice_detail.uom','sale_invoice_detail.uom_factor'])
    ->where('pick_list_detail.pick_list_id',$id)
    ->join('sale_invoices','pick_list_detail.sale_invoice_id','=','sale_invoices.id')
    ->join('sale_invoice_detail','sale_invoice_detail.sale_invoice_id','=','pick_list_detail.sale_invoice_id')
    ->groupBy('sale_invoice_detail.product_id')
    ->get();

错误是这样的 Error upon triggering

1 个答案:

答案 0 :(得分:0)

 $pick_list_items = DB::table('pick_list_detail')
    ->select(['pick_list_detail.*', 'sale_invoice_detail.product_id', 'sale_invoice_detail.product_qty', 'sale_invoice_detail.uom', 'sale_invoice_detail.uom_factor'])
    ->where('pick_list_detail.pick_list_id',$id)
    ->join('sale_invoices', 'pick_list_detail.sale_invoice_id','=','sale_invoices.id')
    ->join('sale_invoice_detail', 'sale_invoice_detail.sale_invoice_id','=','pick_list_detail.sale_invoice_id')
    ->groupBy('sale_invoice_detail.product_id')
    ->groupBy('pick_list_detail.id')
    ->get();

我在group by中添加了另一个缺少的列,即pick_list_detail.id