我有SQL查询,它返回唯一值以及此值在DB中的次数。
SELECT sl_keyword, COUNT(sl_keyword)
FROM search_log
GROUP BY sl_keyword
如何在Yii中使用活动记录执行相同的操作?此代码有效,但不返回计数。只有关键字和语言:
$criteria = new CDbCriteria();
$criteria->select = 'sl_keyword, sl_language, COUNT(sl_keyword) as times';
$criteria->condition = 'sl_datetime >= :date_from AND sl_datetime <= :date_to';
$criteria->group = 'sl_keyword, sl_language';
$criteria->offset = $offset;
$criteria->limit = $itemsPerPage;
$criteria->params = array(':date_from' => $dateFrom, ':date_to' => $dateTo);
$result = self::model()->findAll($criteria);
答案 0 :(得分:0)
尝试这样(我假设您使用的是foreach循环)
foreach($result as $r)
{
$times = $r['times']; // get like this
$sl_keyword = $r['sl_keyword'];
$sl_language = $r['sl_language'];
}