Using the Query Builder that is CDbCommand, how do I select a value that "IS NULL", in MySQL?
Trying to accomplish the following:
SELECT COUNT(*) FROM `console` a LEFT JOIN `console_images` b ON a.id = b.console_id WHERE b.console_id IS NULL
So my query builder looks like this:
$countConsolesMissingImage = Yii::app()->db->createCommand()
->select('count(*)')
->from('console c')
->join('console_images i', 'c.id=i.console_id')
->where('i.console_id IS NULL') // This does NOT work!
->queryRow();
This does not return the results I need, and I couldn’t find an example of the where function defining something as “IS NULL”. Is there no way to do this using the query builder?
That’s weird. I’ve just seen your post and ISNULL() and ‘xxx IS NULL’ are comparison operators.
My first attempt was WHERE xxx IS NULL and it didn’t work on a datetime field, and then ISNULL() worked. Is it a coincidence or is it something special with datetime fields?
Edit: I tried with a varchar field (with default NULL value) and it doesn’t return records even when the field is empty. Anyway, on datetime and int fields, it does work on my phpMyAdmin and in Yii.