Yii的复杂STAT GROUP BY

时间:2011-11-28 17:46:11

标签: php mysql sql yii relational

我正在尝试在模型的关系函数中执行复杂的STAT。

基本上:

'stats' => array(self::STAT, 'Comments', 'post_id', 'select'=>'COUNT(id) AS total_comments, SUM(comment_rating) AS comment_rating')

但是Yii不允许这样做。所以我做了一个self :: HAS_ONE,但Yii在检索模型数据时对ENTIRE查询进行了连接和分组。

我想要让STAT工作,以便为不同的数据执行一个组,或者使原始SQL成为

的效果
SELECT * FROM Posts p LEFT JOIN (SELECT .....) AS comments_data CD ON p.id=CD.post_id

这有可能吗?

1 个答案:

答案 0 :(得分:1)

您需要使用两个STAT关系:

'commentCount'=>array(self::STAT, 'Comment', 'post_id'),
'ratingSum'=>array(self::STAT, 'Comment', 'post_id', 'select'=>'SUM(comment_rating)'),

使用STAT无法将它们组合成一个。