根据条件过滤a SQL table

Filter a SQL table according to a condition

我在 MySQL 中有这样一个 table :

Group Seqno Event
  1    1     A
  1    2     B
  1    3     C
  1    4     B
  1    5     E
  1    6     B
  1    7     D
  1    8     A

我想为事件 = B 的每个组计算从最后(最近的条目)开始的所有行,并且 return 计数为 2 时立即计算所有剩余的行。 输出将是

Group Seqno Event
  1    4     B
  1    5     E
  1    6     B
  1    7     D
  1    8     A

知道如何实现它。

您似乎想要从倒数第二行的所有行"B"?

如果是这样,您可以使用相关子查询:

select t.*
from t
where t.seqno >= (select t2.seqno
                  from t t2
                  where t2.group = t.group and t2.event = 'B'
                  order by t2.seqnum desc
                  limit 1, 1
                 );

处理可能没有"second"序列号的情况,可以使用coalesce():

select t.*
from t
where t.seqno >= coalesce( (select t2.seqno
                            from t t2
                            where t2.group = t.group and t2.event = 'B'
                            order by t2.seqnum desc
                            limit 1, 1
                           ), t.seqno
                         );