简体   繁体   English

应用限制后,可以重新排序SQL选择吗?

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

I'd like to see the last ten rows from our mysql database in ID order. 我想按ID顺序从mysql数据库中查看最后十行。 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? 表达我要运行的查询的正确方法是什么?

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. 请注意所需的子选择后的无辜xxx

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. 您需要类似的内容,因为FROM子句甚至在SELECT之前就已执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM