Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using find and null while OR'ing other values

Tags:

cakephp

I would like to filter certain fields in my database which are Null, 0, or ''. Unfortunately, using NULL in an IN condition fails to return anything...I believe this is due to NULL comparisons in SQL evaluating as UNKNOWN. For example:

$filterField = $this->Model->find('list', array(
     'fields' => array('id','name'),
     'recursive' => 0,
     'conditions' => array('Model.related_string' => array(Null, 0, '')),
     'order' => array('Model.name ASC') 
     )
  );

This always returns no errors and zero rows because the resulting query has SELECT ... WHERE 'Model'.'related_string' IN (NULL, 0, ''). However, if I want to OR the NULL condition separately, I can't seem to do it with PHP's array syntax. I will overwrite the values. For example:

          $conditions['OR'] = array(
             'Model.related_string' => array('', 0),
             'Model.related_string' => NULL);

Failure. This will only search for NULL entries when the value for the 'Model.related_string' key is overwritten. Am I stuck writing two finds?

like image 817
Michael Avatar asked Sep 29 '10 22:09

Michael


People also ask

How do you handle NULL and other special values in tableau?

To filter null dimensions or discrete measures, drag the pill to the Filter shelf and deselect Null. The null value will appear in the list with discrete values, where you can then remove it. When a measure contains null values, they are usually plotted in a view as zero.

How do you check for NULL values in multiple columns?

Filtering NULL from Multiple Columns This can be performed with this simple statement using AND with multiple comparison operators: SELECT primary_author, published_date, title FROM books WHERE ( primary_author IS NOT NULL AND published_date IS NOT NULL );

How do I compare two columns with NULL values in SQL?

Use <=> (null-safe equality operator) negated comparison which returns FALSE in case one of the operands is null but TRUE when both are null and both operands have equal non-null values.

Which operator should you use to find NULL values?

The IS NULL operator is used to test for empty values (NULL values).


1 Answers

Just wrap it in one more array:

$conditions['OR'] = array(
    array('Model.related_string' => array('', 0)),
    array('Model.related_string' => NULL)
);

I'd suggest that if you have that many different values to test against, what you should first and foremost think about is to standardize them to one possible value.

like image 115
deceze Avatar answered Sep 28 '22 06:09

deceze