まず、3回目の参加はまったく必要ありません。 1つの結合で計算を行うことができます:
from (select id
from owner
where date_format(auction_date,'%Y-%m-%d %H:%i:00') = date_format(NOW(),'%Y-%m-%d %H:%i:00')
) as a left join
(select owner_id, max(nb) as maxbid, max(mb) as maxautobi
from auction
group by owner_id
) b
on a.id=b.owner_id;
mb
の2番目に大きい値を取得する 次に、substring_index()
を含むトリックを使用します およびgroup_concat()
:
from (select id
from owner
where date_format(auction_date,'%Y-%m-%d %H:%i:00') = date_format(NOW(),'%Y-%m-%d %H:%i:00')
) as a left join
(select owner_id, max(nb) as maxbid, max(mb) as maxautobi,
substring_index(substring_index(group_concat(mb order by mb desc), ',', 2), ',', -1
) as second_mb
from auction
group by owner_id
) b
on a.id=b.owner_id;
アイデアは、mb
の順序で、値を連結することです。 。次に、リストの2番目の要素を取得します。欠点の1つは、数値で始まる場合でも、値が文字列に変換されることです。