在蛋糕 2.1 中,我需要从包含的记录中求和一个字段。
目前我得到子表上所有记录的总和,但需要按我的主表(员工)的 id 分组。
参加属于员工
$agents = $this->Employee->find('all', array(
'fields' => array('Employee.FullNameNoId'),
'conditions' => $conditions,
'contain' => array(
'Attend' => array(
'conditions' => array(
'Attend.as_date BETWEEN ? AND ?' => array(
date('Y-m-d', strtotime($this->passedArgs['date1'])),
date('Y-m-d', strtotime($this->passedArgs['date2']))
)
),
'fields' => array('sum(Attend.as_labormin) AS total'),
//'group' => array('Attend.employee_id') // This gets sql errors below
)
)
));
尝试了几种 SQL 错误的组合:
'group' => array('Attend.employee.id') // Model "Attend" is not associated with model "Attend"
//Column not found: 1054 Unknown column
'group' => array('employee_id') // Model "Attend" is not associated with model "employee_id"
// Column not found: 1054 Unknown column 'Attend.group' in 'field list'
'group' => array('Employee.id') //Column not found: 1054 Unknown column Attend.group' in 'field list'
表之间的关系很好,我可以得到相关的记录,问题是通过员工ID得到一个总和。
检查了相关字段的 Cakephp SUM,但是使用 SELECT SUM 似乎很麻烦,并且他们遗漏了所需的分组。
你能帮我吗?