Reverse the “natural order” of a MySQL table without ORDER BY?

前端 未结 2 1620
醉话见心
醉话见心 2021-01-14 09:51

I\'m dealing with a legacy database table that has no insertion date column or unique id column, however the natural order of insertion is still valid when examined with a s

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 10:47

    If you're writing an application to process the data, another approach might be to run your current query, then iterate over the returned records from last to first.

    If you have too many records, then you may wish to instead use a view. This is a Database object which can be used to combine data from different tabls, or present a modified view of a single table, amongst other things. In this case, you could try creating a view of your table and add a generated ID column. You could then run SELECT statements against this view ordering by the new column you have added.

    However be aware of the advice from another poster above: the order in which rows are returned without an ORDER BY clause is arbitrary and may change without notification. It would be best to amend your table if at all possible.

    mySQL CREATE VIEW syntax

提交回复
热议问题