我需要两个不同表中两个不同列的总和,即 tbl_bill
以及 tbl_receipt
.
待确认收据
id receipt_no member_id receipt_amount
1 21001 21 20500
2 21002 21 200
tbl\U成员
id name
21 priya
待定发票
id bill_no order_id member_id bill_value
1 30001 30 21 20928
2 0002501 25 21 12030
我要这样回答
member_id member_name receipt_sum bill_sum
21 priya 20700 32958
我试着做左连接,但它的总和增加了一倍。有人能帮我吗?
$adapter = $this->tableGateway->getAdapter();
$orderTable = new TableGateway('tbl_receipt', $adapter);
$sqlSelect = $orderTable->getSql()->select();
$sqlSelect->columns(array('id'=>'id','bill_amount'=>'bill_total','sum_receipt_amount'=>new \Zend\Db\Sql\Expression('SUM(tbl_receipt.receipt_amount)')));
$sqlSelect->join('tbl_member','tbl_member.id = tbl_receipt.member_id', array('member_id'=>'id','member_name'=>'name'),'LEFT');
// $sqlSelect->join('tbl_receipt','tbl_receipt.member_id = tbl_bill.member_id', array('receipt_id'=>new \Zend\Db\Sql\Expression('(tbl_receipt.receipt_amount)')),'LEFT');
$sqlSelect->group('tbl_member.id');
$resultSet = $orderTable->selectWith($sqlSelect);
//echo"<pre>";
// print_r($resultSet); die;
return $resultSet;
1条答案
按热度按时间yzuktlbb1#
在sql中,应该在sum的结果上留下join