1 $query0 = ImGroupUser::find()->where(['gid'=>'56680dfc60b215d62104a4d8'])->select('user_client_id')->all();//AR 2 $query1=ImGroupUser::findAll(['gid'=>'56680dfc60b215d62104a4d8']);//AR 3 $query2=ImGroupUser::findBySql("select user_client_id from im_group_user where gid='56680dfc60b215d62104a4d8'")->all();//AR 4 $query3=ImGroupUser::find()->where(['gid'=>'56680dfc60b215d62104a4d8'])->select('user_client_id')->asArray()->all();//Array 5 $query = new yiidbQuery(); 6 $query4 = $query 7 ->select(['user_client_id']) 8 ->from(['im_group_user']) 9 ->where(['gid'=>'56680dfc60b215d62104a4d8']) 10 ->all();//Array 11 $arr=[]; 12 foreach($query4 as $key=>$value){ 13 // $arr[]=$value->user_client_id; 14 $arr[]=$value['user_client_id']; 15 } 16 var_dump($arr);
AR查询结果默认为AR单条数据的对象,返回AR对象,用13行取对象结果集,返回Array对象用14行取结果集 注意query4查询结果为数组对象
1 $res = ImGroupUser::updateAll(['flag'=>0],['gid'=>$gid,'flag'=>1]);
更新数据库字段值。(将表中所有gid为$gid并且 flag=1的列的flag设为0)
有的时候查询过程较为复杂,使用框架查询效率低下,可以直接使用sql查询:
1 $sql="select c_user.client_id,c_user.bapp_broker_id from c_user 2 where c_user.client_id!='' 3 and c_user.bapp_broker_id!='{$g32}' 4 and c_user.id in(select c_user_id from trade_fund_account where cust_id is not null and update_time between date_sub(now(),interval 10 minute) and now() order by update_time desc)"; 5 $res=Yii::$app->db->createCommand()->setSql($sql)->queryAll();
Yii查询sql也可以这样写
1 $res=Yii::$app->db->createCommand($sql)->queryAll();