How to do IS NULL and IS NOT NULL with Yii 2 ActiveRecord?

后端 未结 5 2024
情话喂你
情话喂你 2021-02-12 10:22

I have a table which has a field `activated_at` timestamp NULL DEFAULT NULL, which means that it can contain a timestamp or it can be null and it\'s

相关标签:
5条回答
  • 2021-02-12 10:56

    for this expression:

    WHERE activated_at IS NULL
    

    try this (it's working):

    ->andWhere(['is', 'activated_at', new \yii\db\Expression('null')]),
    
    0 讨论(0)
  • 2021-02-12 11:02

    this solution check if column_name is empty or NULL

    WHERE (LENGTH(`column_name`) > 0)

    ->andWhere(['>', 'LENGTH(column_name)', 0]) //check if empty or null
    

    Another variant - check only for NULL

    WHERE column_name IS NOT NULL

    ->andWhere(['IS NOT', 'column_name', null]); // check on null
    
    0 讨论(0)
  • 2021-02-12 11:05
    $null = new Expression('NULL');
    
    $query->andFilterWhere(['is not', 'asp_id', $null]);
    

    OR

    $query->andFilterWhere(['is', 'asp_id', $null]);
    
    0 讨论(0)
  • 2021-02-12 11:07

    If i understand right you can use andWhere

     ->andWhere(['not', ['activated_at' => null]])
    

    but andFilterWhere in execute where the related value is not null

    from doc http://www.yiiframework.com/doc-2.0/yii-db-query.html

    andFilterWhere() Adds an additional WHERE condition to the existing one but ignores empty operands.

    0 讨论(0)
  • 2021-02-12 11:17
    // ...WHERE (`status` = 10) AND (`type` IS NULL) AND (`info` IS NOT NULL)
    $query->where([
        'status' => 10,
        'type' => null,
    ])
    ->andWhere(['not', ['info' => null]]);
    
    0 讨论(0)
提交回复
热议问题