简体   繁体   English

具有内部联接的SQL max()

[英]SQL max() with inner joins

I am creating an auction website. 我正在创建一个拍卖网站。 Here people can look for items and place bets on them. 人们可以在这里寻找物品并对其下注。 In the user account area I wish to have a list showing all the items where the user has placed a bid on. 在用户帐户区域中,我希望有一个列表,显示用户对其进行出价的所有项目。

Ofcourse each item can have more than one bet from different users so I only wish to show the one with the highest amount of money. 当然,每个项目可以有多个来自不同用户的赌注,因此我只希望显示最高金额的项目。 This is so that the user can follow all the items on which he placed a bid on, and can track if he is still the one with the highest amount or not. 这样一来,用户就可以关注其出价所在的所有项目,并可以跟踪他是否仍然是出价最高的项目。

This is how my database looks like: 这是我的数据库的样子:

Tabel item                      Tabel itembid                          Tabel user
+=========================+     +================================+     +==============+
|id | title | description |     |id | item_id | user_id | amount |     |id | username |
+=========================+     +================================+     +==============+
| 1 | item1 | ........... |1   *| 1 |    1    |    2    |   10   |*   1| 1 |    me    |
| 2 | item2 | ........... |-----| 2 |    1    |    1    |   15   |-----| 2 |  myself  |
| 3 | item3 | ........... |     | 3 |    2    |    3    |   5    |     | 3 |    I     |
+=========================+     | 4 |    2    |    1    |   10   |     +==============+
                                +================================+

So as shown above, I have 3 tables (item, itembid and user). 因此,如上所示,我有3个表(item,itembid和user)。 As you can see user 'me' has placed 2 bid, once on item 1 and once on item 2. It turns out that he is also currently the one with the highest bids on both items. 如您所见,用户“我”设置了2个出价,一次在项目1上,一次在项目2上。事实证明,他目前也是这两个项目上出价最高的人。

Now user 'myself' placed a bid before on item1 and 'I' placed a bid on item2. 现在,用户“我自己”在item1之前出价,而“我”在item2出价。 However, they are not anymore the ones with the highest bid (user 'me' is). 但是,它们不再是出价最高的用户(用户“我”是)。 Now I need an SQL statement that gives me a list of information (title, description, amount, username) that is a list of all items where I once placed a bid on. 现在,我需要一条SQL语句,该语句为我提供信息列表(标题,描述,金额,用户名),该信息列表是我曾经出价的所有项目的列表。 If I am the one currently with the highest bid for that item, I need to see the title and description of the item together with the amount that I placed for my bet as well as my username. 如果我当前是该项目的最高出价者,则需要查看该项目的标题和说明以及下注的金额和用户名。 Now, if I am not the one with the highest bid, I stil want to see the information of that item but now with the amount and username of the one with the highest bid. 现在,如果我不是出价最高的用户,我仍然想查看该项目的信息,但现在要查看出价最高的用户的数量和用户名。

So an example, looking in the perspective of user 'me', I want to see: 因此,从用户“我”的角度来看一个例子,我想看看:

> item1, 15, me
> item2, 10, me

Now for user 'myself' I wish to see: 现在,对于用户“我自己”,我希望看到:

> item1, 15, me

(since 'me' once placed a bid for item1 but is no longer the one with highest amount, user 'me' is) (由于“我”曾经为item1出价,但不再是出价最高的用户,因此用户“我”就是)

This is what I have so far but isn't working quiet well... 这是我到目前为止所拥有的,但是运行起来并不安静...

SELECT i.title, i.description, ib.amount, u.username
FROM item i
INNER JOIN itembid ib ON i.id = ib.item_id
INNER JOIN user u ON ib.user_id = u.id
WHERE ib.amount = (SELECT max(amount) FROM itembid ib2 WHERE ib2.id = ib.id)
AND ib.user_id = 1

Here it is: 这里是:

SELECT i.title, bmax.amount, u.username
FROM itembid AS b
JOIN (SELECT item_id, MAX(amount) AS amount
      FROM itembid
      GROUP BY item_id) AS bmax
    ON b.item_id = bmax.item_id AND b.amount = bmax.amount
JOIN item AS i ON i.id = b.item_id
JOIN itembid AS ubid ON ubid.item_id = i.item_id
JOIN user AS u ON u.id = b.user_id
WHERE ubid.user_id = :current_user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM