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?
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.
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 );
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.
The IS NULL operator is used to test for empty values (NULL values).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With