How do I change MySQL database insertion order?

3

I want to insert something into my MySQL (PhpMyAdmin) it inserts and above the last post. Example:

Yesterday I inserted Nome: Pedro | Idade: 20 Anos

and it was at the top because it was the first one to be inserted into the database

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

But he has to stay on top of Pedro, the database view order he needs to stay on top.

Is it possible to do this?

    
asked by anonymous 27.10.2018 / 15:11

1 answer

8

There is no such thing as what you are thinking. The database is a mechanism for storing and querying data. You do not establish how it will be stored, this is his problem to do this, the data is only there, no matter how. You insert what you need and it's over, it does not matter as it was inserted.

When you go to see you have to say how you want the results to come back. Since the secret is in SELECT , there you will tell the way information is brought to you.

The question does not give details to help better, but probably would look something like this:

SELECT * FROM tabela ORDER BY id DESC;

See running on SQL Fiddle . Also I've placed GitHub for future reference .

If you have a column named id that is auto incremented. That's usually how you do it. If you do not have a column with a value that is guaranteed in ascending order you can not do what you want in MySQL, that is probably an error. Creating an index to better handle this order can be instrumental in maintaining good performance on many lines.

    
27.10.2018 / 15:45