CodeIgniter Forums
Suggestion: Order by is null condition - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Suggestion: Order by is null condition (/showthread.php?tid=82718)



Suggestion: Order by is null condition - jojo - 08-15-2022

I am using CI 4.2.1 on my project

I needed to use ORDER BY IS NULL ASC/DESC on a datetime from my db.
How about to add the possibility to add a conditional order by is null?

CodeIgniter\Database\BaseBuilder (line 1422)
Previously
$direction = in_array($direction, ['ASC', 'DESC'], true) ? ' ' . $direction : '';


In
$direction = in_array($direction, ['ASC', 'DESC', 'IS NULL ASC', 'IS NULL DESC'], true) ? ' ' . $direction : '';


RE: Suggestion: Order by is null condition - iRedds - 08-16-2022

try like this orderBy('IS NULL', 'ASC', false);


RE: Suggestion: Order by is null condition - salain - 08-17-2022

Hi,
If you are using MySQL NULL values are considered having a lower value than any none NULL value, so order by "fieldName" ASC will give you NULL values first.
And The opposite order by "fieldName" DESC will put the NULL values last.
Regards