41

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,
       ]);
Vikram Pote
  • 4,959
  • 4
  • 31
  • 34

8 Answers8

81

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.

Oldskool
  • 32,791
  • 7
  • 50
  • 64
  • 1
    That does not generate "IS NOT NULL", that generates "NOT (City IS NULL)". @mariovials answer is the correct one here. – jurchiks Apr 13 '18 at 14:23
25
->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],
)
mariovials
  • 544
  • 6
  • 11
13

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.

arogachev
  • 31,868
  • 6
  • 105
  • 113
7
    $items = BadOffer::find()->where(['OR',
                                               ['IS', 'moderator_id', (new Expression('Null'))],
                                               ['moderator_id' => $user->id],
    ]);
Mirocow
  • 301
  • 3
  • 4
4

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
4

In Yii2, we can use any one of the below query to check the null condition

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

or

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

or

->andWhere('city IS NOT NULL')
Ashok Kumar
  • 151
  • 5
3

How to select non-empty columns in MySQL?

use LENGTH :

SELECT col1
FROM table
WHERE LENGTH(col1) > 0

Yii2 :

->andWhere(['>', 'LENGTH(col1)', 0])
sj59
  • 1,684
  • 2
  • 19
  • 22
-3

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

->andFilterWhere(['<>', '`city`', 'null']); 
atrichkov
  • 313
  • 2
  • 6
  • 1
    This may work only by accident - it create condition like `city <> 'null'`which may ignore null values (although it may depend on DBMS) but will also ignore fields with value `null` as string. – rob006 Sep 21 '18 at 07:41