Home > Back-end >  MySQL simple query of table returns results in strange order
MySQL simple query of table returns results in strange order

Time:12-29

My MySQL table is set up with an auto-incrementing primary key. Whenever I

SELECT * FROM [MYTABLE]

The record with the highest primary key value is not displayed last. Is there a reason for this? I can

SELECT * FROM [MYTABLE] ORDER BY [MYTABLE].ID ASC

and the highest is displayed last. Sorry, I am not at liberty to share anything from the database. Perhaps there is some sort of underlying data field (like a record number) not contained in the table that is being used for the sort. This is MySQL 5.7.19 on a Windows server. It seems to me that sorting by a primary key makes more sense than what it's doing. MySQL version 5.7.19

CodePudding user response:

Tables are to be seen as multisets; there is no inherent notion of order between the rows of a table. Mysql is at liberty to return the rows in any order if you haven't specified any presentation order in an ORDER BY clause.

CodePudding user response:

What you want is not applicable. Well, of course, MySquile inherently fetches data no matter how it is stored. Sort the index table each time you change it, list the main key in the table, or sort the call each time. select with ordering

SELECT * FROM [MYTABLE] ORDER BY [MYTABLE].ID ASC

Keep in mind that you can never guarantee the order of an auto-incremented column once you start inserting and removing data, so you shouldn't be relying on any order except that which you specify using ORDER BY in your queries. This is an expensive operation that you are doing, as it requires indexes to be completely re-created, so I wouldn't suggest doing it often.

  • Related