in Yii's criteria how to get count (*)

17,864

Solution 1

Work's with alias. Yii 1.1.11. Fails other

$criteria= new CDbCriteria();
$criteria=array(
            'group'=>'attribute',
            'select'=>'id,count(*) AS cc'
);

Solution 2

This not exactly an answer to your question, but could still be useful for you. If you use the result to only look up one count for one specific attribute, you don't need to fecth an entire array, but use Yii's count method:

$criteria= new CDbCriteria();
$criteria.compare('col_name', $desired_col_value);
$count = X::model()->count($criteria);
Share:
17,864
Hilmi
Author by

Hilmi

Updated on June 05, 2022

Comments

  • Hilmi
    Hilmi almost 2 years

    I'm trying to build a query that has a group by attribute, im truing to get the id and the count it keeps telling me that the count (*) is invalid column name how could i get the count from the group by query ??

  • psycho brm
    psycho brm over 10 years
    So how do I access it then? $x = X::model()->findAll(...); $x[0]->cc is not working..
  • psycho brm
    psycho brm over 10 years
    It does if I add the variable into the model: class X .. { public $cc;. After that, the $x[0]->cc works.