简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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