CAKEPHP 3:在一个语句中选择*和sum()

时间:2017-04-01 08:48:10

标签: cakephp cakephp-3.0

我正在尝试使用SELECT *和SUM()函数从数据库表中获取结果。 sql查询是:

  

SELECT *,SUM(msg_send)AS msg_send FROM msg_campaigns

现在如何在cakephp3中编写此查询。

我正在尝试这个:

$this->loadModel('MsgCampaigns');  
$SmsDetails = $this->MsgCampaigns->find('all',[ 
'conditions'=>['YEAR(date_time)'=>date('Y')],
'fields'=>['msg_send'=>'SUM(msg_send)','msg_failed'=>'SUM(msg_failed)']
]);

但我不知道如何使用SELECT *。请帮忙

1 个答案:

答案 0 :(得分:0)

检查how to use SQL functionshow to select all fields上的CakePHP查询生成器。

$query = $this->MsgCampaigns->find();
$query
    ->select([
        'sum_msg_send' => $query->func()->sum('msg_send'),
        'sum_msg_failed' => $query->func()->sum('msg_failed')
    ])
    // passing the table instance to the `select` function, selects all fields
    ->select($this->MsgCampaigns);

$query->execute();