【问题标题】:Add a 2nd condition inside a WHERE clause?在 WHERE 子句中添加第二个条件?
【发布时间】:2018-02-28 00:35:00
【问题描述】:

我正在尝试在我的 foreach 循环的 WHERE 中添加第二个条件。

$customers = Customers::find()->where(['status' => 1])->orderBy('vip DESC, id ASC')->all();

目前,我有过滤 where status=1 但我还需要添加 where 'rank' 不等于文本“Can”。

我需要 StackOverflow 的原因是我试图在网上找到答案,但是我有一个文本比较和一个我以前没有使用过的新查询方法,所以我不能很好地研究。

如何在 WHERE 子句 "AND 'rank' != 'Can' 中添加附加条件?

如果我不得不猜测它,我会这样写(这可能是错误的)

 $customers = Customers::find()->where(['status' => 1])
    ->andwhere (['rank' !=> 'Can'])
    ->orderBy('vip DESC, id ASC')->all();

【问题讨论】:

    标签: php activerecord yii


    【解决方案1】:

    根据惊人的ActiveQuery documentation,你必须使用andWhere()方法:

    $customers = Customers::find()->where(['status' => 1])
       ->andwhere (['not', ['rank' => 'Can']])
       ->orderBy('vip DESC, id ASC')->all();
    

    或者:

    ->andwhere (['<>', 'rank', 'Can'])
    

    请记住,PHP 没有 !=&gt; 比较运算符!

    【讨论】:

      【解决方案2】:

      在 Yii 中,我认为你需要改变,去哪里,去哪里。 或者试试这个

      $customers = Customers::find()->where("status = 1 and rank!='Can'"])->orderBy('vip DESC, id ASC')->all();
      

      【讨论】:

        猜你喜欢
        • 2016-12-16
        • 2013-12-23
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 2012-07-26
        • 1970-01-01
        相关资源
        最近更新 更多