Can I reorder SQL selections after the limit has been applied?

穿精又带淫゛_ 提交于 2019-12-24 17:19:20

问题


I'd like to see the last ten rows from our mysql database in ID order. Naively, I'd expect to be able to do something like this:

SELECT * FROM ( SELECT * FROM things ORDER BY id DESC LIMIT 10) ORDER BY id ASC

but that isn't valid syntax. What's the correct way of expressing the query I'm trying to run?


回答1:


You got that almost right:

SELECT * 
FROM 
  ( SELECT * FROM things ORDER BY id DESC LIMIT 10) xxx
ORDER BY id ASC

Note the innocent xxx after the subselect which you need.




回答2:


Try:

SELECT * FROM (SELECT * FROM things ORDER BY id DESC LIMIT 10) temp
ORDER BY id ASC

You need something like that because here FROM clause is executed even before the SELECT.



来源:https://stackoverflow.com/questions/3255636/can-i-reorder-sql-selections-after-the-limit-has-been-applied

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!