How to use not equal to inside a Yii2 query

Yii2

Yii2 Problem Overview


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 Solutions


Solution 1 - Yii2

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

Solution 2 - Yii2

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

Solution 3 - Yii2

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))
    ]);

Solution 4 - Yii2

The better and safe way to apply a condition.

Booking::find()->where('tour_id = :tour_id and id != :id', ['tour_id'=> $chk->tour_id, 'id' => $id])->all();

Solution 5 - Yii2

You can use this

$this->find()->where(['resource_id' => $resource_id]) ->andWhere(['>=', 'start_time', $start_time])->all();

Solution 6 - Yii2

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();

Solution 7 - Yii2

        <?= $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 .

Solution 8 - Yii2

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();

Solution 9 - Yii2

In case anyone reading this needs to use a REGEXP or similar, this works in Yii2 (2.0.15.1):

->andWhere(['NOT REGEXP', 'column','[A-Za-z]'])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBloodhoundView Question on Stackoverflow
Solution 1 - Yii2TonyView Answer on Stackoverflow
Solution 2 - Yii2Muhammad ShahzadView Answer on Stackoverflow
Solution 3 - Yii2Kalpesh DesaiView Answer on Stackoverflow
Solution 4 - Yii2SO-userView Answer on Stackoverflow
Solution 5 - Yii2Faizan KhattakView Answer on Stackoverflow
Solution 6 - Yii2Gianpaolo ScrignaView Answer on Stackoverflow
Solution 7 - Yii2Naeem MarzuView Answer on Stackoverflow
Solution 8 - Yii2Mohan PrasadView Answer on Stackoverflow
Solution 9 - Yii2Xavi EsteveView Answer on Stackoverflow