我正在尝试使用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()被写入两次。我该如何解决这个问题?

最佳答案

尝试这个

'fields' => ['sumqty'=>'SUM(Preparations.qty)', 'order_id', 'product_id']

关于mysql - SUM()和CakePHP 3别名的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36983063/

10-11 10:49