简体   繁体   English

如何选择MySQL中的最后一行?

[英]How to select the last n row in MySQL?

with this code I select the first 30 row of the table: 使用此代码我选择表格的前30行:

SELECT * FROM `table` LIMIT 0 , 30

But how to select the last 30, without changing the order? 但是如何在不改变顺序的情况下选择最后30个?

It looks like everyone is missing this part: 看起来每个人都缺少这一部分:

But how to select the last 30, without changing the order? 但是如何在不改变顺序的情况下选择最后30个?

First of all, clearly there is no order in the query provided. 首先,显然在提供的查询中没有顺序。 Assuming the order is ascending on some field this would be the query @DannyFox meant: 假设订单在某个字段上升,这将是查询@DannyFox意味着:

SELECT * FROM T
ORDER BY val
LIMIT 0 , 30

Now imagine we have simplified data, such as a, b, c, d, e and that we want only 3 rows instead of 30: 现在假设我们有简化的数据,例如a, b, c, d, e ,我们只想要3行而不是30行:

SELECT * FROM T
ORDER BY val
LIMIT 3

If this returns: a, b, c, d, e in each row, then he would expect to get c, d, e in that order. 如果这返回:每行中的a, b, c, d, e ,则他希望按此顺序得到c, d, e The query everyone is providing: 每个人提供的查询:

SELECT * FROM T
ORDER BY val desc
LIMIT 3

Would return e, d, c . 将返回e, d, c The problem with this is that it's actually changing the original order, and the OP say he didn't want to change the order. 这个问题是它实际上改变了原来的顺序,并且OP说他不想改变顺序。 So technically, the query that would result in c, d, e is: 从技术上讲,导致c, d, e的查询是:

select * from (
  select * from t
  order by val desc
  limit 3
) s
order by val

Which actually changes the order twice, getting the original order back. 这实际上改变了两次订单,恢复了原始订单。

Since you are trying to avoid ordering, then the solution would be to apply it twice. 由于您试图避免订购,因此解决方案是将其应用两次。

 SELECT * 
   FROM (
            SELECT * 
              FROM `table_name` 
          ORDER BY `column_name` DESC -- maybe id?
             LIMIT 0, 30
         ) `table_aliase`
ORDER BY `column_name` ASC

First you need to specify an order: 首先,您需要指定订单:

SELECT * FROM table
ORDER BY some_id ASC -- ascending order
LIMIT 30

If that query returns the first 30 columns, this one will return the last 30: 如果该查询返回前30列,则此列将返回最后30列:

SELECT * FROM table
ORDER BY some_id DESC -- descending order
LIMIT 30

如果你有一个自动增量键/列,比如id那么这是一个例子

SELECT * FROM `table` ORDER BY id DESC LIMIT 0 , 30;

也许这会起作用: select * from table WHERE id > ((SELECT MAX(id) from table) - 30);

Nothing is said about an order, so you can not use ORDER BY. 没有关于订单的说法,所以你不能使用ORDER BY。 There is a way to get records from a given point, but for that you need to know how many records there are, then use this counted value to provide the limits 有一种方法可以从给定的点获取记录,但为此您需要知道有多少记录,然后使用此计数值来提供限制

SELECT COUNT(*) AS counted FROM table

SELECT * FROM table LIMIT (counted-30),30

Here's my method used with PHP: 这是我用于PHP的方法:

$query = "SELECT * FROM table ORDER BY id DESC LIMIT 3";
$res = mysql_query($query);
$results = array();
while($row = mysql_fetch_assoc($res)){
     $results = $row[field];
}
// Back to original order based from DB
$results = array_reverse(results);

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

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