Can I reorder SQL selections after the limit has b

2019-08-08 10:13发布

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?

2条回答
你好瞎i
2楼-- · 2019-08-08 10:31

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.

查看更多
干净又极端
3楼-- · 2019-08-08 10:49

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.

查看更多
登录 后发表回答