我想找到重复的ISBN作为Distinct,它们的总数量为sum。
我希望 ISBN = 9789 总数量为 509 且 ISBN = 978944 总数量 194
我使用的代码如下所示
`array('fields'=> array('ERPBookdetail.id','ERPBookdetail.item','ERPBookdetail.book_id','ERPBookdetail.isbn','ERPBookdetail.title',
'ERPBookdetail.class','ERPBookdetail.subject','ERPBookdetail.medium','ERPBookdetail.board','ERPBookdetail.edition'
,'ERPBookdetail.db_type','ERPBookdetail.price','ERPOrderDetail.item','ERPOrder.order_id','ERPBookdetail.qty','SUM(ERPBookdetail.qty) as total_quantity'),
'group' => array('ERPBookdetail.isbn'),
'order'=>'total_quantity DESC',
'joins' => array(
array(
'table' => 'orders',
'alias' => 'ERPOrder',
'type' => 'INNER',
'conditions' => array('OR' => array(
array('ERPOrder.confirmation' => 1),array('ERPOrder.confirmation' => 'Yes'),
)),
),
array('table' => 'order_details',
'alias' => 'ERPOrderDetail',
'type' => 'INNER',
'conditions' =>array('ERPOrderDetail.item = ERPBookdetail.item','ERPOrder.order_id = ERPOrderDetail.order_id'),)),)
);`
答案 0 :(得分:2)
只需
即可在mysql中执行此操作 select isbn, sum(qty) as qty from table group by isbn
如果您不想更改mysql查询,那么在php中就可以通过这种方式
for($isbn_rec = 0 ; $isbn_rec < count($your_outputarray_from_query); $isbn_rec++)
{
$new_array[$your_outputarray_from_query['isbn']]['qty'] += $your_outputarray_from_query['qty'];
// if any isbn records comes again then it will be sum up with older isbn number
}
您将获得最终数组,如下所示
array('9789'=> array('qty'=>'509')
'978944'=> array('qty'->'194')
)