Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use not null condition in YII2

Tags:

php

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,        ]); 
like image 809
Vikram Pote Avatar asked Apr 22 '15 11:04

Vikram Pote


2 Answers

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.

like image 190
Oldskool Avatar answered Sep 20 '22 13:09

Oldskool


->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], ) 
like image 44
mariovials Avatar answered Sep 18 '22 13:09

mariovials