MySQL order by primary key

前端 未结 2 1378
梦如初夏
梦如初夏 2021-02-08 07:30

Some SQL servers allow for a generic statement such as ORDER BY PRIMARY KEY. I don\'t believe this works for MySQL, is there any such workaround that would allow f

2条回答
  •  被撕碎了的回忆
    2021-02-08 07:50

    This is too long for a comment.

    SAP does, indeed do this (http://help.sap.com/saphelp_nw04s/helpdata/en/fc/eb3a53358411d1829f0000e829fbfe/content.htm). SQL Server is also based on Sybase, and I don't think Sybase supported this functionality. There are many limitations on the syntax.

    On a query on one table with a primary key, no explicit order by, and no where conditions, MySQL will generally return the results in primary key order. You cannot depend on this functionality, but it might be good enough for your system.

    The big issue would be the use of indexes for the where clause. If there are no indexes on the table, you don't have to worry about it. If there are, you could possibly emulate the behavior with a materialized view:

    select t.*
    from (select t.*
          from table t
         ) t
    where ;
    

    Another option is to force the database engine to use the primary key index. You can do this by using a force index hint. The issue is that you need to know the name of the index.

提交回复
热议问题