我运行什么查询来为SQL表中的每组值选择HIGHEST值?

时间:2011-05-19 23:16:08

标签: php mysql sql

我有一张bids表(例如eBay出价):

bid_id | bid_from_user | bid_for_auction | bid_price
1      | 150           | 1               | 20
2      | 453           | 1               | 30
3      | 42            | 1               | 50
4      | 12            | 2               | 12
5      | 60            | 2               | 20

我需要选择每个bid_for_auction一次及其当前最高出价,因此我的结果应如下所示:

bid_for_auction | bid_price
1               | 50
2               | 20

我将如何做到这一点?

4 个答案:

答案 0 :(得分:3)

使用GROUP BY

SELECT bid_for_auction, MAX(bid_price) AS bid_price
FROM bids GROUP BY bid_for_auction;

答案 1 :(得分:2)

这应该可以解决问题:

SELECT
 `bid_for_auction`,
 MAX(`bid_price`)
FROM `bids`
GROUP BY `bid_for_auction`

答案 2 :(得分:2)

SELECT bid_for_auction, MAX(bid_price)
FROM bids
GROUP BY bid_for_auction

答案 3 :(得分:1)

SELECT bid_for_auction,MAX(bid_price)FROM出价GROUP BY bid_for_auction