views:

36

answers:

3

i have image table, which has 2 or more rows with same date.. now im tring to do order by created_date DESC, which works fine and shows rows same position, but when i change the query and try again, it shows different positions.. and no i dont have any other order by field, so im bit confused on why its doing it and how can i fix it.

can you please help on this.

+5  A: 

To get reproducible results you need to have columns in your order by clause that together are unique. Do you have an ID column? You can use that to tie-break:

ORDER BY created_date DESC, id
Mark Byers
do you think, if i change date format to timestamp would that work too? i mean would timestamp be uniqe always, even if row added one milisecond or same time?
Basit
+1  A: 

I suspect that this is happening because MySQL is not given any ordering information other than ORDER BY created_date DESC, so it does whatever is most convenient for MySQL depending on its complicated inner workings (caching, indexing, etc.). Assuming you have a unique key id, you could do:

SELECT * FROM table t ORDER BY t.created_date DESC, t.id ASC

Which would give you the same result every time because putting a comma in the arguments following ORDER BY gives it a secondary ordering rule that is executed when the first ordering rule doesn't produce a clear order between two rows.

Steven Xu
i accepted his answer, cause he gave answer first, but i voted for you too.. :)
Basit
+1  A: 

To have consistent results, you will need to add at least more column to the 'ORDER BY' clause. Since the values in the created_date column are not unique, there is not a defined order. If you wanted that column to be 'unique', you could define it as a timestamp.

RaleighwoodRacer