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?
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();
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
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]'])
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();
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();
You can use this
$this->find()->where(['resource_id' => $resource_id]) ->andWhere(['>=', 'start_time', $start_time])->all();