MySQL Orderby a number, Nulls last

MysqlSql Order-By

Mysql Problem Overview


Currently I am doing a very basic OrderBy in my statement.

SELECT * FROM tablename WHERE visible=1 ORDER BY position ASC, id DESC

The problem with this is that NULL entries for 'position' are treated as 0. Therefore all entries with position as NULL appear before those with 1,2,3,4. eg:

NULL, NULL, NULL, 1, 2, 3, 4

Is there a way to achieve the following ordering:

1, 2, 3, 4, NULL, NULL, NULL.

Mysql Solutions


Solution 1 - Mysql

MySQL has an undocumented syntax to sort nulls last. Place a minus sign (-) before the column name and switch the ASC to DESC:

SELECT * FROM tablename WHERE visible=1 ORDER BY -position DESC, id DESC

It is essentially the inverse of position DESC placing the NULL values last but otherwise the same as position ASC.

A good reference is here <http://troels.arvin.dk/db/rdbms#select-order_by>

Solution 2 - Mysql

I found this to be a good solution for the most part:

SELECT * FROM table ORDER BY ISNULL(field), field ASC;

Solution 3 - Mysql

NULL LAST

SELECT * FROM table_name ORDER BY id IS NULL, id ASC

Solution 4 - Mysql

Something like

SELECT * FROM tablename where visible=1 ORDER BY COALESCE(position, 999999999) ASC, id DESC

Replace 999999999 with what ever the max value for the field is

Solution 5 - Mysql

You can swap out instances of NULL with a different value to sort them first (like 0 or -1) or last (a large number or a letter)...

SELECT field1, IF(field2 IS NULL, 9999, field2) as ordered_field2
  FROM tablename
 WHERE visible = 1
 ORDER BY ordered_field2 ASC, id DESC

Solution 6 - Mysql

Try using this query:

SELECT * FROM tablename
WHERE visible=1 
ORDER BY 
CASE WHEN position IS NULL THEN 1 ELSE 0 END ASC,id DESC

Solution 7 - Mysql

You can http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce">coalesce</a> your NULLs in the ORDER BY statement:

select * from tablename
where <conditions>
order by
    coalesce(position, 0) ASC, 
    id DESC

If you want the NULLs to sort on the bottom, try coalesce(position, 100000). (Make the second number bigger than all of the other position's in the db.)

Solution 8 - Mysql

For a DATE column you can use:


NULLS last:

ORDER BY IFNULL(`myDate`, '9999-12-31') ASC

Blanks last:

ORDER BY IF(`myDate` = '', '9999-12-31', `myDate`) ASC

Solution 9 - Mysql

SELECT * FROM tablename WHERE visible=1 ORDER BY CASE WHEN `position` = 0 THEN 'a' END , position ASC

Solution 10 - Mysql

To achieve following result :

1, 2, 3, 4, NULL, NULL, NULL.

USE syntax, place -(minus sign) before field name and use inverse order_type(Like: If you want order by ASC order then use DESC or if you want DESC order then use ASC)

SELECT * FROM tablename WHERE visible=1 ORDER BY -position DESC

Solution 11 - Mysql

This works well for me as well.

ORDER BY ISNULL(field), field = 0 ASC;

Returns 1 2 3 0 0 null null

Solution 12 - Mysql

This is working fine:

SELECT * FROM tablename ORDER BY position = 0, position ASC;

position
1 
2
3
0
0

Solution 13 - Mysql

That's simple. You just need to order twice:

  • first step, order NULLs
  • second step, order your field.
SELECT * FROM table_name 
ORDER BY ISNULL(field_name), field_name;

It works with any types, including JSON.

Solution 14 - Mysql

Why don't you order by NULLS LAST?

SELECT * 
FROM tablename
WHERE visible = 1 
ORDER BY position ASC NULLS LAST, id DESC 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonBView Question on Stackoverflow
Solution 1 - MysqlJarredView Answer on Stackoverflow
Solution 2 - Mysqld-_-bView Answer on Stackoverflow
Solution 3 - MysqlSumeetView Answer on Stackoverflow
Solution 4 - MysqlDrewMView Answer on Stackoverflow
Solution 5 - MysqlLangdonView Answer on Stackoverflow
Solution 6 - MysqlRachit PatelView Answer on Stackoverflow
Solution 7 - MysqlSethView Answer on Stackoverflow
Solution 8 - MysqlDanny BeckettView Answer on Stackoverflow
Solution 9 - MysqlYasirPoongadanView Answer on Stackoverflow
Solution 10 - MysqlNishu GargView Answer on Stackoverflow
Solution 11 - MysqljcdsrView Answer on Stackoverflow
Solution 12 - Mysqlvoxl.deView Answer on Stackoverflow
Solution 13 - MysqlRoman SamarskyView Answer on Stackoverflow
Solution 14 - MysqlRedRoverView Answer on Stackoverflow