t1.name,     row_number() over(PARTITION BY t1.id order by t1.name) rowss1,--第一条     row_number() over(PARTITION BY t1.id order by t1.name desc) rowss2--倒数第一条 from t_test t1 where t1.pid= ' 0 ' where rowss1= ' 1 ' or rowss2= ' 1 ' ) GROUP BY id

不分组的话去掉

PARTITION BY t1.id