How to use not equal to inside a Yii2 query

2019-03-11 14:42发布

I want to use a yii2 query in which i want to check a not equal to condition. I tried like this but it didn't give the desired results. how do i do it?

$details        =   MovieShows::find()->where(['movie_id'=>$id])
                    ->andWhere(['location_id'=>$loc_id])
                    ->andWhere(['cancel_date'=>!$date])->all();

标签: yii2
9条回答
老娘就宠你
2楼-- · 2019-03-11 15:22

In addiction to the @tony answer, for those who need to encapsulate a subquery here there's a quick example:

$users = User::find()                  
            ->where([
                'not in',
                'id',
                (new Query())
                    ->select('user_id')
                    ->from(MyTable::tableName())
                    ->where(['related_id'=>$myRelatedId])
            ->all();
查看更多
手持菜刀,她持情操
3楼-- · 2019-03-11 15:22

You can just use the below pasted code:

$details        =   MovieShows::find()->where(['movie_id'=>$id])
                    ->andWhere(['location_id'=>$loc_id])
                    ->andWhere(['not in','cancel_date',$date])->all();
查看更多
我只想做你的唯一
4楼-- · 2019-03-11 15:28
        <?= $form->field($model, 'first_name')->dropDownList(
        arrayhelper::map(user::find()
                                ->where([ 'not in ' ,'first_name', patient::find() ->select([ 'patient_name' ]) ->asArray()  ])
        ->all(),'id','first_name')

Maybe it help you for whom need to use subquery .

查看更多
Rolldiameter
5楼-- · 2019-03-11 15:31

You can also try this:

$details = MovieShows::find()->where(['movie_id'=>$id])
           ->andWhere(['!=', 'cancel_date', $date])->all();

for Greater than

$details = MovieShows::find()->where(['movie_id'=>$id])
               ->andWhere(['>', 'cancel_date', $date])->all();

for less than

$details = MovieShows::find()->where(['movie_id'=>$id])
               ->andWhere(['<', 'cancel_date', $date])->all();

More check here

查看更多
Melony?
6楼-- · 2019-03-11 15:32

Maybe this one help...:)

$query->andFilterWhere([ 'or',
                           ['<=', 'affidavit_cont', 0],
                           ['=', 'affidavit_cont1', 0],
                           ['>', 'affidavit_cont2', 0],
                           ['!=', 'affidavit_cont3', $this->affidavit3],
                           ['in', 'attempt_count', $this->attempted],

                      ]);

$query->andFilterWhere(['like', 'job_invoice.paid_status', '',]);

$query->andFilterWhere(['in', 'status_id', $this->status_id]);

$query->andFilterWhere(['between', 'last_attempt',
    strtotime(Utility::convertDate2Db($this->from_date)),
    strtotime(Utility::convertDate2Db($this->to_date))
    ]);
查看更多
祖国的老花朵
7楼-- · 2019-03-11 15:34

In this case you need to use operator format: [operator, operand1, operand2, ...]. So your code should look like this:

$details = MovieShows::find()
           ->where(['movie_id'=>$id])
           ->andWhere(['location_id'=>$loc_id])
           ->andWhere(['<>','cancel_date', $date])
           ->all();

More about using where method and operator format

查看更多
登录 后发表回答