2015-04-22 6 views
17

Ciao voglio usare la condizione non nulla nella mia query yii2 come dovrei usarlo. non voglio città e stato null.come usare la condizione non nulla in YII2

La mia domanda è

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

Quale dei campi non deve essere nullo? – Oldskool

+0

non voglio città e stato null. –

risposta

40

È possibile utilizzare l'operatore not combinato con i campi che non devono essere nulli per generare un'istruzione SQL IS NOT NULL. In questo modo:

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

Controllare anche gli esempi nello documentation.

10

Una delle opzioni saranno:

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

Controlla documentazione ufficiale per where.

+1

Manca "," dopo "<>" – llioor

+0

@llioor Grazie, corretto. A proposito, puoi suggerire anche la modifica. – arogachev

+1

Non posso perché è solo 1 carattere da cambiare. TW hai dimenticato "," anche nella riga precedente. – llioor

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

ottenere

WHERE column IS NOT NULL

0

uso ->andWhere(['not', ['State' => null]]) o ->andWhere(['is not', 'State', null]);

fare alcun uso:

  1. andFilterWhere, come il 0.123.farà questo filtro essere ignorato
  2. ->andWhere(['<>', 'State', null]) come modulo di richiesta AND State <> null
0

In Yii2, è possibile utilizzare come qui di seguito

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

Come selezionare le colonne non vuote in MySQL?

uso LUNGHEZZA:

SELECT col1 
FROM table 
WHERE LENGTH(col1) > 0 

Yii2:

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