2014-03-30 65 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

相關問題