如何使用不等于Yii2查询内部

Blo*_*und 43 yii2

我想使用一个yii2查询,我想检查一个不等于条件.我试过这样但是没有给出预期的结果.我该怎么做?

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

Ton*_*ony 95

在这种情况下,您需要使用运算符格式:[operator, operand1, operand2, ...].所以你的代码应该是这样的:

$details = MovieShows::find()
           ->where(['movie_id'=>$id])
           ->andWhere(['location_id'=>$loc_id])
           ->andWhere(['<>','cancel_date', $date])
           ->all();
Run Code Online (Sandbox Code Playgroud)

有关使用where方法和运算符格式的更多信息

  • 您也可以使用`!=`作为"不等于"运算符. (12认同)

Muh*_*zad 17

你也可以试试这个:

$details = MovieShows::find()->where(['movie_id'=>$id])
           ->andWhere(['!=', 'cancel_date', $date])->all();
Run Code Online (Sandbox Code Playgroud)

为大于

$details = MovieShows::find()->where(['movie_id'=>$id])
               ->andWhere(['>', 'cancel_date', $date])->all();
Run Code Online (Sandbox Code Playgroud)

小于

$details = MovieShows::find()->where(['movie_id'=>$id])
               ->andWhere(['<', 'cancel_date', $date])->all();
Run Code Online (Sandbox Code Playgroud)

更多检查在这里


小智 8

也许这个帮助...... :)

$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))
    ]);
Run Code Online (Sandbox Code Playgroud)


SO-*_*ser 5

应用条件的更好,更安全的方法.

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