如何在 mariaDB 的某些列中获取具有不同值的最后一行

How get last row with different value in some columns in mariaDB

我需要获取最后一行(例如 10 rows) 在 mariaDB 的某些列(例如 cat 列)中具有不同的值。

id 名字
1 val1 name1
2 val1 name2
3 val2 name3
4 val2 name4
5 val2 姓名5
6 val3 name6
7 val3 name7
8 val3 姓名 8

结果:

id 名字
2 val1 name2
5 val2 姓名5
8 val3 姓名 8

您可以使用 lead():

select t.*
from (select t.*,
             lead(cat) over (order by id) as next_cat
      from t
     ) t
where next_cat is null or next_cat <> cat;

Here 是一个 db<>fiddle.