50

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
  • 5,433
  • 4
  • 33
  • 37

8 Answers8

87

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
  • 34,211
  • 7
  • 53
  • 66
  • 4
    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
38
->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
  • 782
  • 9
  • 12
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
  • 33,150
  • 7
  • 114
  • 117
7
    $items = BadOffer::find()->where(['OR',
                                               ['IS', 'moderator_id', (new Expression('Null'))],
                                               ['moderator_id' => $user->id],
    ]);
Mirocow
  • 340
  • 3
  • 5
5

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');
Ashok Kumar
  • 326
  • 2
  • 8
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
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
  • 2,072
  • 3
  • 22
  • 23
-3

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

->andFilterWhere(['<>', '`city`', 'null']); 
atrichkov
  • 450
  • 3
  • 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