2014-03-30 66 views
1

我想查找除一个用户类型以外的用户表中的所有记录。即我有一个用户表dec_user,其中有一个属性user_type。我想查找除user_type9以外的所有记录。然后我会计算行数。所以,我写为:count在Yii中查找所有记录

$user_type = 9; 
    return count(User::model()->findAll(array("condition"=>"':user_type' != $user_type"))); 

其实我不明白怎么写这个条件。

回答

6

您不需要从数据库检索数组并使用PHP count()函数对其进行计数。

的Yii的方式:

return User::model()->count('user_type <> '.$user_type); 

或使用PARAMS:

return User::model()->count('user_type <> :type', array('type' => $user_type); 

,或者,如果你想建立的SQL查询,使用CommandBuilder的:

return Yii::app()->db->createCommand() 
      ->select('COUNT(*)') 
      ->from('user') 
      ->where('user_type <> '.$user_type) 
      ->queryScalar(); 
+0

真棒,谢谢 – StreetCoder

相关问题