1

我有以下表格:

拍卖台

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。

4

5 回答 5

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
于 2013-06-28T11:37:21.710 回答
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 |
+------------+-------+---------------------+---------+-----+
于 2013-06-28T11:26:29.080 回答
0

询问:

SQLFIDDLE示例

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 |
于 2013-06-28T11:55:55.553 回答
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;

小提琴在这里:小提琴

第三个用户的结果:

ID  TITLE   DESCRIPTION             USERID
1   Test1   DescriptionForTest1     46
2   Test2   DescriptionForTest2     20
3   Test3   DescriptionForTest3     23
于 2013-06-28T12:16:51.057 回答
-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
于 2013-06-28T11:08:00.453 回答