用现有公式分别截取第一个值与最后一个值:sql("xx","select case when rn4 <> 0 then code4 when rn3 <> 0 then code3 when rn2 <> 0 then code2 when rn1 <> 0 then code1 end `code` from (SELECT code1,code2,code3,code4, count(code4) over() rn4 ,count(code3) over() rn3 ,count(code2) over() rn2 ,count(code1) over() rn1 FROM dm_subject_detail where code1 = '"+B4+"' or code2 = '"+B4+"' or code3 ='"+B4+"' or code4 = '"+B4+"'order by code4,code3,code2,code1) t1",1)