简体   繁体   English

SQL:联接2个表,并且从第一个表的每个条件仅获得一个结果

[英]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.

相关问题 如何编写SQL查询以将两个表与映射表连接起来并获得格式化的输出(每个结果仅一行)? - How to write SQL Query to join two tables with mappings table and get formatted output with only one row per result? 如何内部联接2个SQL表,但仅从第二个表中获取第一个结果? - How do I inner join 2 SQL tables, but only take the first result from the second table? 如果第二个表与连接条件不匹配,则laravel从第一个表获取结果 - laravel get result from first table if second table did not match join condition 使用联接从三个表中获取结果,其中一个表包含多个结果 - Get result from three tables using join from which one table contain multiple result Laravel:连接两个表从表一中获取信息,并从表二中获取第一个相关图像 - Laravel : Join two tables get the info from table one and get the first related image from table two 使用 where 条件连接 2 个表,但显示 table2 中的第一行 - JOIN 2 tables with where condition, but show first row from table2 在MySQL中联接三个表,并获得每个ID一行(第一个表中的每个实体一行) - Join three table in MySQL and get one row per id (one row per each entity in first table) 连接两个表,条件是第一个表 - Join two tables with condition on the first table 每个内部联接表如何只获得一个结果 - How to get only one result per inner joined table 仅从具有where条件的联接表中选择第一条记录 - Selecting first record only from join table with a where condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM