表LIMIT 8、3中的SELECT ID
结果为8,9,10
但我需要10,9,8
你该怎么做?如果您添加“ ORDER BY ID DESC”,则得到3,2,1
将查询放入子选择中,然后在外部选择中颠倒顺序:
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指出这一点!