MySQL Forums
Forum List  »  Newbie

Re: if statment in ORDER BY
Posted by: Roland Bouman
Date: August 03, 2005 02:52AM

Ok, i guess what you really mean is "Normally, NULLS appear at the top in an ASC ORDER BY, and I want them to appear at the bottom, how do i do that?"

I know some SQL dialects have a syntax construct to control NULL sorting, ex Oracle. There you could say:

ORDER BY A, B NULLS LAST, C

I dont think MySQL has this. You can work around it if you know the logical maximum value for a particular column, you could use that in the IFNULL function. So, assume your B column is a INT UNSIGNED, than, the logical maximum is 4294967295 + 1. That is, 4294967295 is the maximum for a INT UNSIGNED,

So, ORDER BY A,IFNULL(B,4294967296),C would give you the desired result.

Of course, this workaround is not rigourous, there can be cases where you cannot have constant bigger than logical maximum of you column. For example, The INT UNSIGNED example would fail if it had been a BIGINT.

Options: ReplyQuote


Subject
Written By
Posted
August 02, 2005 09:04AM
August 02, 2005 12:51PM
August 03, 2005 02:35AM
Re: if statment in ORDER BY
August 03, 2005 02:52AM
August 03, 2005 02:59AM
August 03, 2005 03:19AM
August 03, 2005 03:45AM
August 03, 2005 04:40AM
August 03, 2005 04:09AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.