php – Yii:如何在GROUP BY与CDbCriteria之前进行ORDER BY?

前端之家收集整理的这篇文章主要介绍了php – Yii:如何在GROUP BY与CDbCriteria之前进行ORDER BY?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用这样的查询
SELECT * FROM (
    SELECT * FROM message ORDER BY added_on DESC
) as m WHERE receiver_id = 2 GROUP BY sender_id ORDER BY priority_id DESC;

我知道如何使用findAllBysql来做到这一点:

$this->findAllBysql(
    'SELECT * FROM (
        SELECT * FROM message ORDER BY added_on DESC
     ) as m WHERE receiver_id = :receiverId 
     GROUP BY sender_id 
     ORDER BY priority_id DESC',array('receiverId' => $userId));

但是我想知道是否有任何方法可以使用CDbCriteria执行此操作,因为以下代码当然不起作用:

$criteria = new CDbCriteria();
$criteria->condition = 'receiver_id = :receiverId';
$criteria->group = 'sender_id';
$criteria->order = 'priority_id DESC,added_on DESC';
$criteria->params = array('receiverId' => $userId);

谢谢.

如果你有一个相当复杂的SQL查询,最好将它保存在sql中,而不是在Active Record中编写它.是的,它限制了数据库的可移植性,但是您可能没有这些查询,并且更简单,更易维护的代码总是获胜.

猜你在找的PHP相关文章