Zend Framework, Zend_Db_Table ORDER BY sorting NULL as last instead of first

If you have a group of fields, some of which may be null (such as an address, in which the 1st line of the address may or may not be filled in) then you may want to sort them all so that if null appears in a field, it’s sorted to the bottom of the list as opposed to the top (which is the default behaviour)

$select = $table->select();
$rowset = $table->fetchAll($select->order('IF(ISNULL(line1),1,0), line1', 'line2', 'city', 'country'));

I grabbed the MySQL code from the MySQL Forums

Add comment