MySQL 从子查询顺序中选择 [英] MySQL select from subquery order

查看:35
本文介绍了MySQL 从子查询顺序中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有下表:

CREATE TABLE `docs` ( 
    `id` int(6) unsigned NOT NULL, 
    `rev` int(3) unsigned NOT NULL, 
    `content` varchar(200) NOT NULL, 
--
    PRIMARY KEY (`id`) 
) 

并执行以下查询:

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?

推荐答案

是的,如果您只使用

select * 
from ( 
    select * 
    from docs 
    order by rev desc 
) as `rows`

那么它会和往常一样但是不应该在子查询中使用 ORDER BY.某些外部查询中使用的子查询,并且该外部查询无论如何都必须进行排序,因此对子查询进行排序是没有意义的

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

如果在子查询中使用 TOP 或 LIMIT,则需要在子查询中使用 ORDER.但这不是标准的 SQL

你应该这样使用它

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

这篇关于MySQL 从子查询顺序中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆