[英]count findAll records in Yii
I want to find all records from user table except one user type. 我想从用户表中查找除一种用户类型外的所有记录。 ie I have a user table
dec_user
where there is an attributes user_type
. 即我有一个用户表
dec_user
,其中有一个属性user_type
。 I want to find all records except user_type
9
. 我想查找除
user_type
9
之外的所有记录。 Then I'll count the number of rows. 然后,我将计算行数。 So, I wrote as:
所以,我这样写道:
$user_type = 9;
return count(User::model()->findAll(array("condition"=>"':user_type' != $user_type")));
Actually, I do not understand how to write this condition. 其实,我不明白该怎么写。
You do not need to retrieve an array from database and count it using the PHP count()
function. 您不需要从数据库检索数组并使用PHP
count()
函数对其进行count()
。
The Yii way: Yii方式:
return User::model()->count('user_type <> '.$user_type);
or by using params: 或使用参数:
return User::model()->count('user_type <> :type', array('type' => $user_type);
or, if you want to build the SQL query, use commandBuilder: 或者,如果要构建SQL查询,请使用commandBuilder:
return Yii::app()->db->createCommand()
->select('COUNT(*)')
->from('user')
->where('user_type <> '.$user_type)
->queryScalar();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.