我正在尝试使用SELECT *和SUM()函数从数据库表中获取结果。 sql查询是:
SELECT *,SUM(msg_send)AS
msg_send
FROMmsg_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 *。请帮忙
答案 0 :(得分:0)
检查how to use SQL functions和how 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();