How mysql order the rows with same values

前端 未结 5 1858
庸人自扰
庸人自扰 2020-12-17 09:54

In my database I have some records where I am sorting by what happens to be the same value:

| col1 | timestamp              |
| row1 | 2011-07-01 00:00:00            


        
5条回答
  •  猫巷女王i
    2020-12-17 10:25

    The answer is: No, the order won't be consistent. I faced the same issue and solved it by adding another column to the order section. Be sure that this column is unique for each record like 'ID' or whatever it is.

    In this case, you must add the 'ID' field to your table which is unique for each record. You can assign it 'AI' (auto increment) so that you are not going to deal with the maintenance.

    After adding the 'ID' column, update the last part of your query like:

    SELECT mt.*
    FROM my_table mt
    ORDER BY mt.timestamp ASC, mt.id DESC
    

提交回复
热议问题