Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 NOT IN condition not working

I use this for query not in:

 $usertypes=Usertype::find()->where(['not in ','user_type_id',['2,3,4']])->all();

Error:

Database Exception – yii\db\Exception

Undefined offset: 1 Failed to prepare SQL: SELECT * FROM usertype WHERE user_type_id NOT IN :qp0

also tried the array format as ['2','3','4'] but not works?What is the issue?

like image 486
Jackhad Avatar asked Jun 01 '16 10:06

Jackhad


4 Answers

Try This :

$usertypes=Usertype::find()
           ->where(['not in','user_type_id',[2,3,4]])
           ->all();

OR :

$usertypes=Usertype::find()
               ->where(['NOT',['user_type_id'=>[2,3,4]]])
               ->all();

Refer : http://www.bsourcecode.com/yiiframework2/select-query-model/#In-Condition

like image 76
Yasin Patel Avatar answered Oct 12 '22 02:10

Yasin Patel


Maybe remove space character from 'not in '?

$usertypes=Usertype::find()->where(['not in', 'user_type_id', ['2,3,4']])->all();
like image 23
exec Avatar answered Oct 12 '22 04:10

exec


you can use "not" word or "<>"

 $usertypes=Usertype::find()->where(['not',['user_type_id'=>['2,3,4']]])->all();

or this

 $usertypes=Usertype::find()->where(['<>',['user_type_id'=>['2,3,4']]])->all();
like image 42
sami.ghaffari Avatar answered Oct 12 '22 04:10

sami.ghaffari


Try to use ->andFilterWhere instead of where ->where

Try this:

$usertypes = Usertype::find()
           ->andFilterWhere(['NOT IN','user_type_id',[2,3,4]])
           ->all();
like image 27
Pedro Machadinho Avatar answered Oct 12 '22 04:10

Pedro Machadinho