52

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();
Juan Antonio
  • 1,659
  • 2
  • 18
  • 28
Bloodhound
  • 2,720
  • 8
  • 31
  • 66

9 Answers9

108

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

Dzhuneyt
  • 7,039
  • 11
  • 56
  • 108
Tony
  • 5,537
  • 3
  • 23
  • 21
22

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

Muhammad Shahzad
  • 7,778
  • 20
  • 71
  • 123
10

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))
    ]);
David Newcomb
  • 10,026
  • 3
  • 42
  • 56
Kalpesh Desai
  • 1,307
  • 14
  • 16
5

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();
SO-user
  • 1,328
  • 1
  • 19
  • 40
2

You can use this

$this->find()->where(['resource_id' => $resource_id]) ->andWhere(['>=', 'start_time', $start_time])->all();
Faizan Khattak
  • 852
  • 6
  • 21
1

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();
1
        <?= $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 .

Naeem Marzu
  • 156
  • 2
  • 12
0

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();
Juan Antonio
  • 1,659
  • 2
  • 18
  • 28
Mohan Prasad
  • 572
  • 8
  • 30
0

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

Xavi Esteve
  • 1,094
  • 2
  • 18
  • 32