php 如何在 YII2 中使用非空条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29796329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:35:40  来源:igfitidea点击:

how to use not null condition in YII2

phpyii2

提问by Vikram Pote

Hi i want to use not null condition in my yii2 query how should i use that. i don't want city and state null.

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

My query is

我的查询是

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

回答by Oldskool

You can use the notoperator combined with the fields that should not be null to generate a IS NOT NULLSQL statement. Like this:

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

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

Also check the examples in the documentation.

还要检查文档中的示例。

回答by mariovials

->where(['IS NOT', 'column', null]);

get

得到

WHERE column IS NOT NULL

WHERE column IS NOT NULL

回答by arogachev

One of the options will be:

选项之一是:

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

Check official docs for where.

检查官方文档的位置

回答by Mirocow

    $items = BadOffer::find()->where(['OR',
                                               ['IS', 'moderator_id', (new Expression('Null'))],
                                               ['moderator_id' => $user->id],
    ]);

回答by Stephen Tang

use ->andWhere(['not', ['State' => null]])or ->andWhere(['is not', 'State', null]);

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

Do no use :

不要使用:

  1. andFilterWhere, as the nullwill make this filter be ignored
  2. ->andWhere(['<>', 'State', null])as it form query AND State <> null
  1. andFilterWhere, 因为这null将使这个过滤器被忽略
  2. ->andWhere(['<>', 'State', null])因为它形成查询 AND State <> null

回答by sj59

How to select non-empty columns in MySQL?

如何在 MySQL 中选择非空列?

use LENGTH:

使用长度

SELECT col1
FROM table
WHERE LENGTH(col1) > 0

Yii2 :

一二:

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

回答by ashokkumarjuly

In Yii2, you can use like below

在 Yii2 中,您可以像下面这样使用

1) ->andWhere(['NOT', ['city' => null]])
2) ->andWhere(['<>', ['city' => null]])
3) ->andWhere('city IS NOT NULL')

回答by atrichkov

This work for me, but only when pass null as string

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

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