我正在使用CakePHP,似乎无法翻译我在工作台上所做的SQL查询。这正是我想要做的,但我似乎无法使它发挥作用。
这是我对MySQL的查询:
select
docs.id, docs.schoolId, schools.name, docs.datesubmitted, docs.status,
uploads.iddocs,
COUNT(*),
COUNT(case uploads.staff_checked when 1 then 1 else null end)
FROM
docs as docs, schools as schools,
uploads as uploads
where
docs.schoolId = schools.schoolId AND
docs.id = uploads.iddocs AND
docs.status ="Submitted"
group by
docs.id;
我需要做的是组合三个表:文档,学校和上传。
这两项指标:COUNT(case when)
和COUNT(*)
表示审核的文档数量(COUNT(case when)
)和需要审核的文档总数(COUNT(*)
)。
到目前为止,MySQL正在展示我的需求。有人可以弄清楚如何在Cake中做到这一点吗?
答案 0 :(得分:0)
尝试以下查询
$this->virtualFields = array(
'count_case_uploads_staff_checked_when_1_then_1_else_null_end' => 'COUNT(case uploads.staff_checked when 1 then 1 else null end)',
'count' => 'COUNT(*)',
);
$options = array(
'fields' => array(
'Doc.id',
'Doc.schoolId',
'schools.name',
'Doc.datesubmitted',
'Doc.status',
'uploads.iddocs',
'Doc.count_case_uploads_staff_checked_when_1_then_1_else_null_end',
'Doc.count',
),
'joins' => array(
array(
),
),
'conditions' => array(
'Doc.schoolId = schools.schoolId',
'Doc.id = uploads.iddocs',
'Doc.status' => 'Submitted',
),
'group' => array(
'Doc.id',
),
);
$data = $this->find('all', $options);