简体   繁体   English

如何运行哪个查询以选择SQL表中每个值组的最高值?

[英]What query do I run to select the HIGHEST value for EACH group of values in an SQL table?

I have a table of bids (like an eBay bids, for example): 我有一张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

I need to select each bid_for_auction only once with its highest current bid, so my result should look like this: 我需要选择每个bid_for_auction只与最高当前的出价一次 ,所以我的结果应该是这样的:

bid_for_auction | bid_price
1               | 50
2               | 20

How would I go about doing this? 我将如何去做呢?

Use GROUP BY 使用GROUP BY

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

This should do the trick: 这应该可以解决问题:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM