I have a MySQL table Section which has a column ordering. Some of the values of ordering are numbers and they are mutually exclusive (no two numbers are the same) and others are simple NULL.
Example:
ID Ordering
-- --------
1 NULL
2 2
3 5
4 NULL
5 NULL
6 3
7 NULL
Now I want to sort this table and get the following result:
(ROW NUMBER) ID Ordering
------------ -- -------- ------------
1 4 NULL
2 2 2
3 6 3
4 5 NULL
5 3 5
6 7 NULL
7 1 NULL
I.e. I want the rows that have a non-NULL value to appear in the given ordering, but the ones that have a NULL value to appear in the remaining ones, in random order (not predictable).
I am sure there are always enough NULL's to fill up the empty spaces.
I was thinking about first getting the ones with an order (WHERE ordering IS NOT NULL) and then the ones without an order (WHERE ordering IS NULL) and then somehow create an extra column that transforms the ones with NULL into a number that doesn't appear in the former.
Any help would be very much appreciated. I am using this in a project where there are sections that have a fixed position, but others can set to have a random order. Every time the page loads the random sections should be displayed, well, randomly.