简体   繁体   English

MySQL从子查询顺序中选择

[英]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. 那么它将与以往一样,但是不应在子查询中使用ORDER BY。 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. 如果在子查询中使用TOP或LIMIT,则需要在子查询中使用ORDER。 But that's not standard SQL 但这不是标准的SQL

You should use it this way 你应该用这种方式

SELECT * 
FROM ( 
    SELECT * 
    FROM docs 
) AS `rows` ORDER BY rev DESC;

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

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