我正在尝试使用SELECT
进行SUM
,但我对别名有疑问。 CakePHP 3在这里使用。
在我的控制器中,我这样做:
$preparations->find('all',
[ 'fields' => ['SUM(Preparations.qty) as sumqty', 'order_id', 'product_id'],
'conditions' => ['order_id IN ' => $ids],
'contain' => ['Products'],
'group' => 'product_id'
]);
但我对此的查询是:
SELECT SUM(Preparations.qty) as sumqty AS SUM(`Preparations__qty`) AS `sumqty`, Preparations.order_id AS `Preparations__order_id`, Preparations.product_id AS `Preparations__product_id`
SUM()
写了两次。我该如何解决这个问题?
答案 0 :(得分:1)
试试这个
'fields' => ['sumqty'=>'SUM(Preparations.qty)', 'order_id', 'product_id']