Is it possible to insert a new row at top of MySQL table?

后端 未结 5 838
别跟我提以往
别跟我提以往 2020-12-10 05:43

All rows in MySQL tables are being inserted like this:

1
2
3

Is there any way how to insert new row at a top of table so that table looks l

5条回答
  •  醉梦人生
    2020-12-10 06:15

    The order in which the results are returned when there's no ORDER BY clause depends on the RDBM. In the case of MySQL, or at least most engines, if you don't explicitly specify the order it will be ascending, from oldest to new entries. Where the row is located "physically" doesn't matter. I'm not sure if all mysql engines work that way though. I.e., in PostgreSQL the "default" order shows the most recently updated rows first. This might be the way some of the MySQL engines work too.

    Anyway, the point is - if you want the results ordered - always specify sort order, don't just depend on something default that seems to work. In you case you want something trivial - you want the users in descending order, so just use:

    SELECT * FROM users ORDER BY id DESC
    

提交回复
热议问题