开发者

Mysql Insert data into table have a arranges question?

I found a weard problem with my MySQL DB.

sometime when I insert new data into it, the way it arranges the data is like a stack, for example

4 (newest)

3

2

1 (oldest) .开发者_JAVA百科..

how can I make it arranged like this?

1 (newest)

2

3

4 (oldest)

thanks all.


SELECT *
FROM TABLE
ORDER BY ID

You have to remember that when viewing/selecting data from a table without any ORDER BY specified does not garuantee any specific order.

The way you are view the data (unordered) can be due to any one of a lot of factos (the database engine, schema, page storage, page fragmentation, indexes, primary keys, or simply execution plan optimization).


The SQL standards specifically states that tables do not have a "natural" order. Therefore, the database engine is free to return a request without an ORDER BY in any order it wants to. The order may change from one request to another because most engines choose to return the data in whatever order they can get it to you the most rapidly.

It follows, therefore, that if you want the data out in a particular order you must include a column in your table whose job is to proxy for the order in which you added records to the table. Two common ways of doing this are using an autoincrement field which will be in numerical order from oldest to newest record, and a TIMESTAMP column which does just what it says. Once you have such a column you can use ORDER BY ColumnName when searching to get an ordered result set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜