SQL:连接2个表,并从第一个表中获得每个条件只有一个结果

时间:2013-06-28 10:58:34

标签: mysql sql join multiple-tables

我有以下表格:

拍卖表

Id | Title | Description 
------------------------
1  | Test1 | DescriptionForTest1
2  | Test2 | DescriptionForTest2
3  | Test3 | DescriptionForTest3

出价-表

AuctionId | UserId | Bidding
----------------------------
1         | 2      | 10
1         | 32     | 24
1         | 2      | 30
1         | 46     | 50
2         | 13     | 5
2         | 20     | 10
and so on...

现在我想要一个输出,类似于

AuctionId | Title | Description | UserId
----------------------------------------
1         | Test1 | Desc...     | 46
2         | Test2 | Desc...     | 20

每次拍卖我需要一行,其中UserId的出价最高。

我知道我可以使用2个SQL语句执行此操作。首先获得所有拍卖,然后使用MAX(竞标)获得第二次拍卖。但是我在一个声明中需要这个,我很想得到一个有效的SQL语句。

有人可以帮我吗?

我使用MySQL作为DBMS。

5 个答案:

答案 0 :(得分:1)

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 |
+------------+-------+---------------------+---------+-----+

答案 1 :(得分:1)

草莓的答案是一种方式。这是第二种方式。尝试两者,看看哪一个更快。索引和表格大小会有很大的不同。

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

答案 2 :(得分:0)

查询:

<强> 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

结果:

| AUCTIONID | TITLE |         DESCRIPTION | USERID |
----------------------------------------------------
|         1 | Test1 | DescriptionForTest1 |     46 |
|         2 | Test2 | DescriptionForTest2 |     20 |

答案 3 :(得分:0)

它适用于这个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

第三位用户的结果:

ID  TITLE   DESCRIPTION             USERID
1   Test1   DescriptionForTest1     46
2   Test2   DescriptionForTest2     20
3   Test3   DescriptionForTest3     23

答案 4 :(得分:-1)

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

或尝试加入

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