Đầu tiên, bạn không cần tham gia thứ ba nào cả. Bạn có thể thực hiện phép tính của mình trong một lần tham gia:
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;
Nhận giá trị lớn thứ hai cho mb
sau đó sử dụng một thủ thuật, liên quan đến substring_index()
và 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;
Ý tưởng là nối các giá trị với nhau, sắp xếp theo mb
. Sau đó, lấy phần tử thứ hai của danh sách. Một nhược điểm là giá trị được chuyển đổi thành chuỗi ký tự, ngay cả khi nó bắt đầu dưới dạng số.