user-defined variables
を使用する1つのオプションは次のとおりです マーチャントのグループごとに行番号を確立し、契約順に並べられた各グループの2番目にフィルタリングします。
select *
from (
select *,
@rn:=if(@prevMerchantId=merchantid,
@rn+1,
if(@prevMerchantId:=merchantid, 1, 1)
) as rn
from yourtable cross join (select @rn:=0, @prevMerchantId:=null) t
order by merchantId, contractid desc
) t
where rn = 2