How do I change MySQL database insert order?

Question:

I want that when inserting something in my MySQL (PhpMyAdmin) it inserts and on top of the last post. Example:

Yesterday I entered Nome: Pedro | Idade: 20 Anos

and it was on top for being the first to be inserted into the database.

Today I want to insert> Nome: João | Idade: 21 Anos

But it has to be on top of Pedro, the order of the database view it needs to be on top.

Is it possible to do that?

Answer:

There is no such thing as you are thinking. The database is a data storage and query mechanism. You don't establish how it will be stored, that's his problem to do, the data is just there, no matter what. You enter what you need and that's it, it doesn't matter how it was entered.

When you go to consult, you need to say how you want the results to come back. Therefore, the secret is in SELECT , there you will tell the form of information to be brought to you.

The question doesn't give details in order to help better, but it would probably go something like this:

SELECT * FROM tabela ORDER BY id DESC;

See working in SQL Fiddle . Also posted on GitHub for future reference .

If you have a column called id that is auto-incremented. That's usually how you do it. If you don't have a column with a value that is guaranteed to be in ascending order you can't do what you want in MySQL, anyway that's probably already an error. Creating an index to better handle this order can be critical to maintaining good performance in databases with many rows.

Scroll to Top