【问题标题】:How to use not equal to inside a Yii2 query如何在 Yii2 查询中使用不等于
【发布时间】:2015-09-11 12:58:12
【问题描述】:

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

我尝试过这样,但没有得到预期的结果。我该怎么做?

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

【问题讨论】:

标签: yii2


【解决方案1】:

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

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

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

【讨论】:

  • 您也可以使用!=作为“不等于”运算符。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    你也可以试试这个:

    $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();
    

    More查看这里

    【讨论】:

      【解决方案4】:

      也许这有帮助...:)

      $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))
          ]);
      

      【讨论】:

      • 我从来没有见过这样的做法,但真的很喜欢。点赞:)
      【解决方案5】:

      您可以使用下面粘贴的代码:

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

      【讨论】:

        【解决方案6】:

        沉迷于@tony 的答案,对于那些需要封装子查询的人来说,这里有一个简单的例子:

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

        【讨论】:

          【解决方案7】:

          你可以用这个

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

          【讨论】:

            【解决方案8】:
                    <?= $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')
            

            也许它对需要使用 subquery 的人有所帮助。

            【讨论】:

              【解决方案9】:

              如果阅读本文的人需要使用 REGEXP 或类似工具,这适用于 Yii2 (2.0.15.1):

              -&gt;andWhere(['NOT REGEXP', 'column','[A-Za-z]'])

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-01-23
                • 1970-01-01
                • 1970-01-01
                • 2023-02-11
                • 2020-05-29
                • 2015-08-17
                • 2021-12-11
                • 1970-01-01
                相关资源
                最近更新 更多