How to use not equal to inside a Yii2 query

后端 未结 9 1662
無奈伤痛
無奈伤痛 2021-02-01 01:20

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?



        
相关标签:
9条回答
  • 2021-02-01 01:38

    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();
    
    0 讨论(0)
  • 2021-02-01 01:45

    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

    0 讨论(0)
  • 2021-02-01 01:48

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

    0 讨论(0)
  • 2021-02-01 01:49

    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();
    
    0 讨论(0)
  • 2021-02-01 01:49

    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();
    
    0 讨论(0)
  • 2021-02-01 01:56

    You can use this

    $this->find()->where(['resource_id' => $resource_id]) ->andWhere(['>=', 'start_time', $start_time])->all();
    
    0 讨论(0)
提交回复
热议问题