MySQL中的分组最大值列:自联接

时间:2015-11-16 18:24:01

标签: mysql group-by

MySQL表,包含姓名,电子邮件,auction_id和出价。我在尝试使用GROUP by auction_id时,从最高出价行获取MAX出价和电子邮件地址。

我可以获得退回的最高出价金额,但返回的电子邮件地址不是MAX出价。

SELECT email, MAX(bid) FROM `kisd_wdm_bidders` GROUP BY auction_id 

2 个答案:

答案 0 :(得分:3)

你必须SELECT T1.auction_id,T1.email,T1.bid FROM kisd_wdm_bidders AS T1 INNER JOIN ( SELECT auction_id, MAX(bid) AS max_bid FROM kisd_wdm_bidders GROUP BY auction_id ) AS T2 ON T1.auction_id = T2.auction_id AND T2.max_bid =T1.bid (不准确)

{{1}}

希望这有帮助。

答案 1 :(得分:1)

通常不要使用SELECT中未在聚合中使用的列。看看herehere。也试试这个:

SELECT aa.email, _aa.max_bid
FROM kisd_wdm_bidders AS aa
INNER JOIN (
    SELECT auction_id, MAX(bid) AS max_bid
    FROM kisd_wdm_bidders
    GROUP BY auction_id
) AS _aa
ON aa.auction_id = _aa.auction_id AND aa.bid = _aa.max_bid;