[英]SQL: Join 2 tables and get only one result per condition from the first table
I have the following tables: 我有以下表格:
Auction-table 拍卖表
Id | Title | Description
------------------------
1 | Test1 | DescriptionForTest1
2 | Test2 | DescriptionForTest2
3 | Test3 | DescriptionForTest3
and 和
Bids-Table 投标-表
AuctionId | UserId | Bidding
----------------------------
1 | 2 | 10
1 | 32 | 24
1 | 2 | 30
1 | 46 | 50
2 | 13 | 5
2 | 20 | 10
and so on...
Now I want a output, similar to this 现在我想要一个类似的输出
AuctionId | Title | Description | UserId
----------------------------------------
1 | Test1 | Desc... | 46
2 | Test2 | Desc... | 20
I need one row per auction with the UserId with the highest Bidding. 我需要使用最高出价的UserId进行一次拍卖。
I know that I could do this with 2 SQL-Statements. 我知道我可以使用2个SQL语句来做到这一点。 First getting all auctions and second with MAX(Bidding). 首先获得所有拍卖,其次获得MAX(出价)。 But I need this in one statement and I'm stucking to get a working SQL-Statement. 但是我只需要一个语句就可以了,我一直坚持要获得一个有效的SQL语句。
Could anyone help me please? 有人可以帮我吗?
I'm using MySQL as DBMS. 我正在使用MySQL作为DBMS。
DROP TABLE IF EXISTS auction;
CREATE TABLE auction
(auction_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Title VARCHAR(12) NOT NULL UNIQUE
,Description VARCHAR(30) NOT NULL
);
INSERT INTO auction VALUES
(1 ,'Test1','DescriptionForTest1'),
(2 ,'Test2','DescriptionForTest2'),
(3 ,'Test3','DescriptionForTest3');
DROP TABLE IF EXISTS bids;
CREATE TABLE bids
(auction_id INT NOT NULL
,user_id INT NOT NULL
,bid INT NOT NULL
,PRIMARY KEY (auction_id,user_id,bid)
);
INSERT INTO bids VALUES
(1 ,2 ,10),
(1 ,32 ,24),
(1 ,2 ,30),
(1 ,46 ,50),
(2 ,13 ,5),
(2 ,20 ,10);
SELECT a.*
, b.user_id
, b.bid
FROM auction a
JOIN bids b
ON b.auction_id = a.auction_id
JOIN
( SELECT auction_id, MAX(bid) max_bid FROM bids GROUP BY auction_id) c
ON c.auction_id = b.auction_id
AND c.max_bid = b.bid;
+------------+-------+---------------------+---------+-----+
| auction_id | Title | Description | user_id | bid |
+------------+-------+---------------------+---------+-----+
| 1 | Test1 | DescriptionForTest1 | 46 | 50 |
| 2 | Test2 | DescriptionForTest2 | 20 | 10 |
+------------+-------+---------------------+---------+-----+
Strawberry's answer is one way. 草莓的答案是一种方法。 Here is a second way. 这是第二种方法。 Try them both and see which one is faster on your setup. 尝试将它们都试一下,看看哪种设置更快。 Indexes and table sizes will make a big difference. 索引和表大小将有很大的不同。
SELECT High_Bid.AuctionId, A.Title, A.Description, High_Bid.UserID FROM
(
SELECT * FROM
(
SELECT * FROM bids_table
ORDER BY AuctionId, Bidding DESC
) AS B1
GROUP BY AuctionId
) AS High_Bid
JOIN
auction_table AS A
ON High_Bid.AuctionId=A.Id
Query: 查询:
SQLFIDDLEExample SQLFIDDLEExample
SELECT t.*
FROM (
SELECT a.Id AS AuctionId,
a.Title,
a.Description,
(SELECT b.UserId
FROM Bids b
WHERE b.AuctionId = a.Id
ORDER BY b.Bidding DESC
LIMIT 1) as UserId
FROM Auction a) t
WHERE T.UserID is not null
Result: 结果:
| AUCTIONID | TITLE | DESCRIPTION | USERID |
----------------------------------------------------
| 1 | Test1 | DescriptionForTest1 | 46 |
| 2 | Test2 | DescriptionForTest2 | 20 |
It works with this sql: 它与此SQL一起工作:
select ID, Title, Description, UserId from Auction a
right join Bids b on a.ID = b.AuctionId
right join (select AuctionId, max(Bidding) as Bidding from Bids group by AuctionId) c on b.AuctionId = c.AuctionId and b.Bidding = c.Bidding;
Fiddle is here: fiddle 小提琴在这里: 小提琴
Result with a third user: 第三个用户的结果:
ID TITLE DESCRIPTION USERID
1 Test1 DescriptionForTest1 46
2 Test2 DescriptionForTest2 20
3 Test3 DescriptionForTest3 23
SELECT MAX(BINDING),A.AUCTION_ID,A.DESCRIPTION,A.TITLE,B.USER_ID
FROM AUCTION_TABLE A,BIDS_TABLE B
WHERE A.AUCTION_ID=B.AUCTION_ID
OR TRY WITH JOINS 或尝试加入
SELECT MAX(BINDING),A.AUCTION_ID,A.DESCRIPTION,A.TITLE,B.USER_ID
FROM AUCTION_TABLE A
INNER JOIN BIDS_TABLE B ON A.AUCTION_ID=B.AUCTION_ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.