【问题标题】:how to use not null condition in YII2如何在 YII2 中使用非空条件
【发布时间】:2015-06-30 00:54:46
【问题描述】:

嗨,我想在我的 yii2 查询中使用非空条件,我应该如何使用它。 我不希望城市和州为空。

我的查询是

$query = new Query;             
      $query->select('ID, City,State,StudentName')                                  
                                ->from('student')                               
                                ->where(['IsActive' => 1])                                                                                                          
                                ->orderBy(['rand()' => SORT_DESC])
                                ->limit(10);                                
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => false,
       ]);

【问题讨论】:

  • 哪些字段不应该为空?
  • 我不希望城市和州为空。

标签: php yii2


【解决方案1】:

其中一个选项是:

$query = new Query;             
$query->select('ID, City,State,StudentName')
    ->from('student')
    ->where(['IsActive' => 1])
    ->andWhere(['<>', 'City', null])
    ->andWhere(['<>', 'State', null])
    ->orderBy(['rand()' => SORT_DESC])
    ->limit(10);

查看where的官方文档。

【讨论】:

  • 你在''之后缺少了","
  • @llioor 谢谢,已修复。顺便说一句,你也可以建议编辑。
  • 我不能,因为它只有 1 个字符可以更改。 TW,您也忘记了前一行中的“,”。
  • 这个查询是错误的。在 SQL 中,不能使用比较运算符来比较 NULL 值。正确的方法是使用“IS NULL”或“IS NOT NULL”条件。
【解决方案2】:

您可以使用not 运算符结合不应为空的字段来生成IS NOT NULL SQL 语句。像这样:

$query = new Query;             
$query->select('ID, City,State,StudentName')
      ->from('student')                               
      ->where(['IsActive' => 1])
      ->andWhere(['not', ['City' => null]])
      ->andWhere(['not', ['State' => null]])
      ->orderBy(['rand()' => SORT_DESC])
      ->limit(10);

还可以查看documentation 中的示例。

【讨论】:

  • 不生成“IS NOT NULL”,生成“NOT (City IS NULL)”。 @mariovials 的答案是正确的。
【解决方案3】:
    $items = BadOffer::find()->where(['OR',
                                               ['IS', 'moderator_id', (new Expression('Null'))],
                                               ['moderator_id' => $user->id],
    ]);

【讨论】:

    【解决方案4】:
    ->where(['IS NOT', 'column', null]);
    

    得到

    WHERE column IS NOT NULL

    你也可以用,打字比较快

    ->where('column IS NOT NULL')
    

    在复杂查询中

    ->where(['AND',
      'column1 IS NOT NULL', // works
      ['IS NOT', 'column2', null], // works
      ['column3' => $value],
    )
    

    【讨论】:

      【解决方案5】:

      使用-&gt;andWhere(['not', ['State' =&gt; null]])-&gt;andWhere(['is not', 'State', null]);

      不要用:

      1. andFilterWhere,因为 null 将使此过滤器被忽略
      2. -&gt;andWhere(['&lt;&gt;', 'State', null]) 形成查询 AND State &lt;&gt; null

      【讨论】:

        【解决方案6】:

        在 Yii2 中,我们可以使用以下任何查询来验证 null 条件,

        ->andWhere(['NOT', ['city' => null]]);
        

        ->andWhere(['<>', ['city' => null]]);
        

        ->andWhere('city IS NOT NULL');
        

        【讨论】:

          【解决方案7】:

          如何在 MySQL 中选择非空列?

          使用 LENGTH

          SELECT col1
          FROM table
          WHERE LENGTH(col1) > 0
          

          Yii2:

          ->andWhere(['>', 'LENGTH(col1)', 0])
          

          【讨论】:

            【解决方案8】:

            这对我有用,但仅当将 null 作为字符串传递时

            ->andFilterWhere(['<>', '`city`', 'null']); 
            

            【讨论】:

            • 这可能只是偶然的——它会创建像city &lt;&gt; 'null'这样的条件,它可能会忽略空值(尽管它可能取决于DBMS),但也会忽略值null作为字符串的字段。跨度>
            猜你喜欢
            • 1970-01-01
            • 2017-10-01
            • 2022-11-28
            • 1970-01-01
            • 2016-01-25
            • 2015-04-18
            • 1970-01-01
            • 2015-08-10
            • 1970-01-01
            相关资源
            最近更新 更多