In my database I have some records where I am sorting by what happens to be a NULL value:
| col1 | col2 |
| row1 | NULL |
| row2 | NULL |
SELECT ... ORDER BY col2
Even though all the values are the same (NULL), I would expect MySQL to return the results in a consistent order, but they are apparently random. I run the query a hundred times and every so often the results are returned in a different order. I can’t find any MySQL documentation on this. Is there a bug? Or is this a documented “feature”?
This is the expected behaviour. The database engine is free to return the rows in any order as long as the “Order By” is honoured. If you want consistency then try:
Where some-unique-id is a column in the table that is unique.