MySQL select from subquery order
If I have following table:
CREATE TABLE `docs` (
`id` int(6) unsigned NOT NULL,
`rev` int(3) unsigned NOT NULL,
`content` varchar(200) NOT NULL,
--
PRIMARY KEY (`id`)
)
and execute following query:
select *
from (
select *
from docs
order by rev desc
) as `rows`
will the order of returned rows be the same as order of inner query?
Can this be guaranteed, generally speaking?
yes, If you are only using
select *
from (
select *
from docs
order by rev desc
) as `rows`
then it will be same as always But using ORDER BY in subquery should not be done. Subquery used in some outer query, and that outer query will have to do ordering anyway, so there's no point ordering the subquery
if you use TOP or LIMIT in the subquery, you will need to use ORDER in Subquery. But that's not standard SQL
You should use it this way
SELECT *
FROM (
SELECT *
FROM docs
) AS `rows` ORDER BY rev DESC;
链接地址: http://www.djcxy.com/p/94334.html
上一篇: 你可以有,如果
下一篇: MySQL从子查询顺序中选择