在MySQL 3.x应用程序中,我试图按照CakePHP语句的顺序使用CASE语句。简单的选择如下:
$articles = $this->Articles->find()
->where($conditions)
->order(function ($exp, $q) {
return $exp->addCase(
[
$q->newExpr()->gt('Articles.modified', (new Time())->subDays(365)) // article has been updated in the last x days
],
['priority'], # values matching conditions
['string'] # type of each value
);
})
->limit(15)
->all();生成以下SQL:
SELECT `Articles`.`id` AS `Articles__id`, ....
FROM `articles` `Articles`
WHERE (`publish` < :c0 AND `Articles`.`publish` > :c1)
ORDER BY CASE WHEN `Articles`.`modified` > :c2 THEN :param3 END LIMIT 15这个案例陈述是不正确的,因为它缺少了DESC的命令,它应该在“结束”之后出现--参见这个小提琴:
http://sqlfiddle.com/#!9/8df161/5
我不确定这是否是CakePHP如何处理案件的一个限制?
此外,我需要在case语句之后的第二个订单通过‘发布’desc命令。
发布于 2020-05-30 10:47:55
传递给Query::order()的表达式必须生成ORDER BY子句所需的所有内容,包括direction关键字。
如果所使用的表达式不支持该表达式,则可以使用或,这将相应地追加相应的方向关键字。
$query = $this->Articles->find();
$query
->where($conditions)
->orderDesc(
$query->newExpr()->addCase(/* ... */)
)
// ...另请参阅
https://stackoverflow.com/questions/62101190
复制相似问题