在单个值上应用group乘以该值

时间:2016-08-30 05:40:06

标签: php codeigniter

我试图从4个表中使用连接获取SUM(),并在最后一个组中获取每个客户的结果。问题是如果每个表具有更多的记录但是失败并且如果任何表只有一个记录则显示双重结果,则查询工作正常。例如,如果表格 A 有2条价格为200和300的记录,则会显示500条正确但是如果表格BCD有单个单一记录,价格B = 200,C = 200,D = 200,查询将表B的结果显示为400表C为400,表D为400.请帮我解决什么错误。

$query = $this -> db -> select('SUM(orders.amount_aed) AS Cash_Incoming, SUM(loan_order.amount_aed) AS Laon_Incoming, SUM(order_banks.amount) AS Bank_Incoming, SUM(order_deposit.amount_aed) AS Deposit_Incoming, orders.*, loan_order.*, order_banks.*, order_deposit.*, customers.*')
            -> from('customers')
            -> join('orders', "orders.dated BETWEEN '$from' AND '$to' AND customers.id=orders.customer_id", 'INNER')
            -> join('loan_order', "loan_order.dated BETWEEN '$from' AND '$to' AND customers.id=loan_order.customer_id", 'INNER')
            -> join('order_banks', "order_banks.dated BETWEEN '$from' AND '$to' AND '$to' AND customers.id=order_banks.customer_id", 'INNER')
            -> join('order_deposit', "order_deposit.dated BETWEEN '$from' AND '$to' AND customers.id=order_deposit.customer_id", 'INNER')
            -> group_by('customers.id')
            -> get();
        return $query -> result();

0 个答案:

没有答案