如何获取mysql中最后一条记录之前的最后一条记录?

时间:2022-10-04 10:40:22

How can I get record 34 of a table if the last record is record 35 ?

如果最后一条记录是35,那么如何获得表格的记录34?

2 个解决方案

#1


11  

If you know there are 35 records, you want limit 1 offset 34.

如果您知道有35条记录,则需要限制1偏移34。

If you want to get the 2nd last element from any set, you can invert the order of the set and select one element, offset one element. You can implement this yourself by first selecting the first two elements of the inversely ordered set, and then reverse the set and select the first element:

如果要从任何集合中获取倒数第二个元素,可以反转集合的顺序并选择一个元素,偏移一个元素。您可以通过首先选择反向排序集的前两个元素来自己实现,然后反转集并选择第一个元素:

select * from
  (select * from my_table order by id desc limit 2) table_alias
order by id limit 1

#2


17  

Simplest method

最简单的方法

SELECT * FROM tab ORDER BY col DESC LIMIT 1,1

This will pick one record starting with the 2nd LIMIT 1,1 means skip first and pick next

这将从第二个LIMIT开始选择一个记录1,1意味着先跳过然后选择下一个

The order by will have to be done so that last is first The col mentioned will most probably be id

必须完成订单,以便最后是第一个提到的col很可能是id

If you know the number of the record you want however why can't you just select where id=34?

如果您知道所需记录的编号,为什么不能选择id = 34的位置?

#1


11  

If you know there are 35 records, you want limit 1 offset 34.

如果您知道有35条记录,则需要限制1偏移34。

If you want to get the 2nd last element from any set, you can invert the order of the set and select one element, offset one element. You can implement this yourself by first selecting the first two elements of the inversely ordered set, and then reverse the set and select the first element:

如果要从任何集合中获取倒数第二个元素,可以反转集合的顺序并选择一个元素,偏移一个元素。您可以通过首先选择反向排序集的前两个元素来自己实现,然后反转集并选择第一个元素:

select * from
  (select * from my_table order by id desc limit 2) table_alias
order by id limit 1

#2


17  

Simplest method

最简单的方法

SELECT * FROM tab ORDER BY col DESC LIMIT 1,1

This will pick one record starting with the 2nd LIMIT 1,1 means skip first and pick next

这将从第二个LIMIT开始选择一个记录1,1意味着先跳过然后选择下一个

The order by will have to be done so that last is first The col mentioned will most probably be id

必须完成订单,以便最后是第一个提到的col很可能是id

If you know the number of the record you want however why can't you just select where id=34?

如果您知道所需记录的编号,为什么不能选择id = 34的位置?