如何在Yii2查询中使用不等于

如何在Yii2查询中使用不等于,yii2,Yii2,我想使用一个yii2查询,其中我想检查一个不等于条件 Booking::find()->where('tour_id = :tour_id and id != :id', ['tour_id'=> $chk->tour_id, 'id' => $id])->all(); 我试着这样做,但没有达到预期的效果。我该怎么做 $details = MovieShows::find() ->where(['movie_id'=>$id]) ->

我想使用一个
yii2
查询,其中我想检查一个不等于条件

Booking::find()->where('tour_id = :tour_id and id != :id', ['tour_id'=> $chk->tour_id, 'id' => $id])->all();
我试着这样做,但没有达到预期的效果。我该怎么做

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

在这种情况下,您需要使用运算符格式:
[运算符,操作数1,操作数2,…]
。因此,您的代码应该如下所示:

$details = MovieShows::find()
           ->where(['movie_id'=>$id])
           ->andWhere(['location_id'=>$loc_id])
           ->andWhere(['<>','cancel_date', $date])
           ->all();
$details=MovieShows::find()
->其中(['movie\u id'=>$id])
->andWhere(['location\u id'=>$loc\u id])
->andWhere(['','cancel_date',$date])
->全部();

关于使用where方法和运算符格式

应用条件的更好、更安全的方法

Booking::find()->where('tour_id = :tour_id and id != :id', ['tour_id'=> $chk->tour_id, 'id' => $id])->all();
您也可以尝试以下方法:

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

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

$details = MovieShows::find()->where(['movie_id'=>$id])
               ->andWhere(['<', 'cancel_date', $date])->all();
$details=MovieShows::find()->其中(['movie\u id'=>$id])
->和where(['也许这一个有帮助…:)

$query->andFilterWhere(['或',
['',宣誓书内容2',0],
['!=',“宣誓书”,$this->宣誓书3],
['in','trust\u count',$this->trusted],
]);
$query->andFilterWhere(['like','job\u invoice.paid\u status','','');
$query->andFilterWhere(['in','status\u id',$this->status\u id]);
$query->andFilterWhere(['between','last\u尝试',
strotime(实用工具::convertDate2Db($this->from_date)),
strotime(实用工具::convertDate2Db($this->to_date))
]);

您只需使用以下粘贴的代码即可:

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

在@tony answer中,对于那些需要封装子查询的人来说,这里有一个简单的例子:

$users = User::find()                  
            ->where([
                'not in',
                'id',
                (new Query())
                    ->select('user_id')
                    ->from(MyTable::tableName())
                    ->where(['related_id'=>$myRelatedId])
            ->all();

你可以用这个

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

如果任何阅读此文件的人需要使用REGEXP或类似文件,则此文件适用于Yii2(2.0.15.1):


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

您也可以使用
!=
作为“不相等”操作符。为什么不直接查看文档而不是进行这种猜测呢?我从来没有见过这样的操作,但确实很喜欢。Upvote:)
        <?= $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')