Mysql 搜索最后 3 条记录是否存在相同的顺序

Mysql search if last 3 records exist with same order

我有一个 table 一列数字

35
24
7
8
11
35
9
11
6
6
23
23
34
5
14
31
26
2
16
0
6
23
23

我想查看最后3条记录

6
23
23

存在于之前的记录中,但顺序相同,结果为下一个数字。

在我的示例中:

6
23
23

存在相同的顺序,结果必须是:34

假设您的号码按名为 id 的 auto_incremented 列排序,那么此查询应该有效:

select numbers 
from vista 
where id = (
    select b.id + 3 from (
      select t1.id, t1.numbers t1val, t2.numbers t2val, t3.numbers t3val 
      from vista t1 
      join vista t2 on t1.id = t2.id-1 
      join vista t3 on t1.id = t3.id-2 
      where t1.id = (select max(id) - 2 from vista)
    ) a
    join (
      select t1.id, t1.numbers t1val, t2.numbers t2val, t3.numbers t3val 
      from vista t1 
      join vista t2 on t1.id = t2.id-1 
      join vista t3 on t1.id = t3.id-2 
      where t1.id < (select max(id) - 2 from vista)
    ) b 
  on a.t1val = b.t1val 
  and a.t2val = b.t2val 
  and a.t3val = b.t3val 
  and a.id <> b.id
) 
order by id limit 1;

如果 id 列不包含序列,您可以使用适当的行编号查询生成序列。

Sample SQL Fiddle