我应该运行什么查询来为 SQL 表中的每组值选择最高值?
我有一个出价
表(例如 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
我将如何去做呢?
I have a table of bids
(like an eBay bids, for example):
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_price
1 | 50
2 | 20
How would I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 GROUP BY
Use GROUP BY
这应该可以解决问题:
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