我应该运行什么查询来为 SQL 表中的每组值选择最高值?

发布于 2024-11-08 02:29:12 字数 547 浏览 0 评论 0原文

我有一个出价表(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里°也失望 2024-11-15 02:29:12

使用 GROUP BY

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

Use GROUP BY

SELECT bid_for_auction, MAX(bid_price) AS bid_price
FROM bids GROUP BY bid_for_auction;
巡山小妖精 2024-11-15 02:29:12
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
天荒地未老 2024-11-15 02:29:12

这应该可以解决问题:

SELECT
 `bid_for_auction`,
 MAX(`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`
如果没结果 2024-11-15 02:29:12

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文