没有DESC的MySQL反向顺序 [英] MySQL reverse order without DESC

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

问题描述

从表LIMIT 8、3中选择ID

SELECT id FROM table LIMIT 8, 3

结果为8,9,10

但是我需要10,9,8

but I need 10,9,8

您该怎么做?如果添加"ORDER BY id DESC",则得到3,2,1

How can you do this? If you add "ORDER BY id DESC" it gets 3,2,1

推荐答案

将查询放入子选择中,然后在外部选择中颠倒顺序:

Put your query in a subselect and then reverse the order in the outer select:

SELECT id from (
    SELECT id FROM table ORDER BY id LIMIT 8, 3
) AS T1 ORDER BY id DESC

测试数据:

CREATE TABLE table1 (id INT NOT NULL);
INSERT INTO table1 (id) VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11);

SELECT id from (
    SELECT id FROM table1 ORDER BY id LIMIT 8, 3
) AS T1 ORDER BY id DESC

结果:

10
9
8

请注意,子查询中的ORDER BY是必需的,否则顺序是不确定的.感谢Lasse指出这一点!

Note that the ORDER BY in the subquery is required otherwise the order is undefined. Thanks to Lasse for pointing this out!

这篇关于没有DESC的MySQL反向顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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