mysql - 为 order by 子句准备 cakephp 案例

标签 mysql cakephp

我准备了一条满足我要求的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/

相关文章:

php - 使用 2 个表选择查询

php - 一个 View (网页)中来自多个模型的数据

php - CakePHP 3.0 - $this->Model->get($id) 引发 MySQL 错误

mysql - 从相关表中检索数据 cakephp

mysql - 如果表中的任何值不为空,则仅显示此非空值。否则只显示空值

android - 将数据从 mysql 的 listview JSON 发送到新 Activity

php - PHPMyAdmin 中的 session 时区

cakephp - 从 CakePHP 中的连接表获取数据

mysql - 使用 CakePHP 的 set()/save() 函数,如何根据现有值的增量保存值?

php - 图层导航菜单