MySQL限制按降序排列 [英] MySQL limit from descending order

查看:83
本文介绍了MySQL限制按降序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写查询以使用相同的"LIMIT(from),(count)",但得到的结果却是反向的?

Is it available to write a query to use same "LIMIT (from), (count)", but get result in backwards?

例如,如果表中有8行,并且我想分两步获取5行,我将: 第一步查询:

In example if I have 8 rows in the table and I want to get 5 rows in two steps I would: first step query:

select * from table limit 0, 5

第一步结果:

first 5 rows;

第二步查询:

select * from table limit 5, 5

第二步结果:

last 3 rows;

但是我想反之亦然.我的意思是从第一步开始我要最后3行,从第二步我要 5行第一行.谢谢您的回答

But I want to get it vice versa. I mean from the first step I want last 3 rows and from the second I want 5 first rows. Thank you for your answer

推荐答案

否,您不应该这样做.没有ORDER BY子句,您不应该依赖于结果在查询之间的顺序相同.它在测试过程中可能效果很好,但顺序不确定,以后可能会中断.使用订购依据.

No, you shouldn't do this. Without an ORDER BY clause you shouldn't rely on the order of the results being the same from query to query. It might work nicely during testing but the order is indeterminate and could break later. Use an order by.

SELECT * FROM table1 ORDER BY id LIMIT 5

顺便说一句,获取最后三行的另一种方法是颠倒顺序并选择前三行:

By the way, another way of getting the last 3 rows is to reverse the order and select the first three rows:

SELECT * FROM table1 ORDER BY id DESC LIMIT 3

即使结果集中的行数不总是8,这也将始终有效.

This will always work even if the number of rows in the result set isn't always 8.

这篇关于MySQL限制按降序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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