agerber85
agerber85

Reputation: 73

MySQL simple query of table returns results in strange order

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.

Upvotes: 0

Views: 51

Answers (1)

Amin Zayeromali
Amin Zayeromali

Reputation: 323

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.

Upvotes: 1

Related Questions