SUM()的问题和CakePHP 3的别名

时间:2016-05-02 12:58:45

标签: mysql cakephp sum cakephp-3.0

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

1 个答案:

答案 0 :(得分:1)

试试这个

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