在SUM()中添加Where语句 - Laravel Eloquent

时间:2014-07-06 00:39:50

标签: laravel eloquent

我有这个查询,它可以正常工作,除了我想在SUM()函数中添加一个WHERE语句,我只需要几条记录来总结我需要的东西。

DB :: raw('SUM(table_order.quantity * products.unit_price)as total')`这里我想添加WHERE语句,如“WHERE orders.status = 0”但不确定我应该在哪里添加此行...

这是我到目前为止所做的查询:

 $table_order = DB::table('table_order')->select('orders.id as  orderID','orders.status as orderStatus','products.unit_price as unitPrice','table_order.quantity as qty','tables.table_nr as table_nr','tables.status as table_status','employers.name as employer',DB::raw('SUM(table_order.quantity * products.unit_price) as total '))
            ->join('orders', 'table_order.order_id', '=', 'orders.id')
            ->join('employers', 'orders.waitress_id', '=', 'employers.id')
            ->join('products', 'table_order.product_id', '=', 'products.id')
            ->join('tables', 'orders.table_nr', '=', 'tables.table_nr','right')
            ->orderBy('table_nr')
            ->groupBy('table_nr')
//            ->where('orders.status', '=', 0)
            ->get();
        return $table_order;

0 个答案:

没有答案