简体   繁体   中英

how to use not null condition in YII2

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.

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

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

$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 .

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

get

WHERE column IS NOT NULL

You can also use, it is faster to type

->where('column IS NOT NULL')

In complex query

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

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 .

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

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

Do no use :

  1. andFilterWhere , as the null will make this filter be ignored
  2. ->andWhere(['<>', 'State', null]) as it form query AND State <> null

In Yii2, we can use any of the queries below to verify the null condition,

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

or

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

or

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

How to select non-empty columns in MySQL?

use LENGTH :

SELECT col1
FROM table
WHERE LENGTH(col1) > 0

Yii2 :

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM