[英]SQL - extract column from subquery
I'm working on a database meant for auctions and I would like get the id of all the winning bids. 我正在使用一个用于拍卖的数据库,我想获得所有中标的ID。 The hard part is extracting it from a subquery that returns 2 columns: the id and the amount.
困难的部分是从返回2列的子查询中提取它:id和数量。 It looks something like this:
SELECT id FROM Bid WHERE id IN (Select ID,max(amount) FROM Bid group by bid.idAuction)
看起来是这样的:
SELECT id FROM Bid WHERE id IN (Select ID,max(amount) FROM Bid group by bid.idAuction)
Can I somehow extract just one column from the subquery? 我可以以某种方式从子查询中仅提取一列吗? Any other sugestions to do this task are helpfull too.
其他建议执行此任务也很有帮助。
Thank you! 谢谢!
Your query is close, but you need a correlated subquery to make this work: 您的查询已经结束,但是您需要一个相关的子查询才能完成此工作:
SELECT b.id
FROM Bid b
WHERE b.amount = (SELECT max(amount)
FROM Bid b2
WHERE b2.idAuction = b.idAuction
);
SELECT id, maxBid.MAmount, Bid.Amount
FROM Bid
INNER JOIN (Select ID,max(amount) mamount FROM Bid group by bid.idAuction) MaxBid
on MaxBid.ID = Bid.ID
RDBMS and SQL operate most effectively in SET based operations. RDBMS和SQL在基于SET的操作中最有效地运行。 So in this case we generate a set based on ID and max bid.
因此,在这种情况下,我们会根据ID和最高出价生成一个集合。 We then join it back to the base set so that only the max bids are treturned.
然后,我们将其重新加入基本集,以便仅调高最高出价。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.