SQL max() with inner joins

前端 未结 1 1745
情深已故
情深已故 2021-01-28 00:46

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

相关标签:
1条回答
  • 2021-01-28 01:40

    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
    
    0 讨论(0)
提交回复
热议问题