我准备了一条满足我要求的sql语句,但我需要转换该sql cakePHP 模型中的语句查找条件我不知道如何准备该条件,请帮忙 我
SQL 查询:
select id,text,is_file,order from kmp_contents
where parent_id=1873
order by case
when is_file=1 then text
when is_file=0 then order
end asc;
最佳答案
你可以尝试:
$this->KmpContent->find(
'conditions' => array('KmpContent.parent_id' => 1873),
'fields' => array('KmpContent.id', 'KmpContent.text', 'KmpContent.is_file', 'KmpContent.order'),
'order' => array('KmpContent.case ASC', 'WHEN is_file=1 THEN KmpContent.text WHEN is_file=0 THEN KmpContent.order ASC')
);
order
键将所提供数组中提供的条件包装在 ORDER BY
语句周围。
如果这不起作用,您可以随时使用 Model::query()
用于复杂的查询。
关于mysql - 为 order by 子句准备 cakephp 案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19153897/