I have a web page which has news data in one table.
In many cases I use following SQL:
SELECT * FROM table ORDER BY insertDate DESC
to order.
ID|priority|insertDate
1 |NULL |2012-09-16
2 |NULL |2012-09-17
3 |NULL |2012-09-18
5 |NULL |2012-09-19
5 |NULL |2012-09-20
4 |1 |2010-05-10 - this is way back in the future
But user wants to prioritize 1 news. And if I use
SELECT * FROM table ORDER BY priority ASC ,insertDate DESC
It doesn’t work correctly, how do I must use ORDER to get result
ID|priority|insertDate
4 |1 |2010-05-10
1 |NULL |2012-09-16
2 |NULL |2012-09-17
3 |NULL |2012-09-18
5 |NULL |2012-09-19
5 |NULL |2012-09-20
Use coalesce to set a valid value to Null priority rows:
Edited
Re-reading your question I see that right order is DESC and not ASC:
Also, notice @yshavit comment. To improve performance you can split query in two selects first one with not null values.
Remember than when you create this new field you can set to it a default value, this will avoid
coalesceandunion: