I'm working with a bids
table for a marketplace and am trying to get the highest bids of a user. A user is able to bid multiple times (with increasing amounts), therefore I only want the highest bid for each listing item.
+----+------------+---------+--------+
| id | listing_id | user_id | amount |
+----+------------+---------+--------+
| 1 | 1 | 1 | 5 |
| 2 | 2 | 1 | 10 |
| 3 | 2 | 1 | 15 |
+----+------------+---------+--------+
I'd like the end result to be:
+----+------------+---------+--------+
| id | listing_id | user_id | amount |
+----+------------+---------+--------+
| 1 | 1 | 1 | 5 |
| 3 | 2 | 1 | 15 |
+----+------------+---------+--------+
I've tried the following query:
select bids.id, bids.listing_id, bids.user_id, max(bids.amount) as amount
from bids
group by listing_id;
But this gives me the following (to me) strange result:
+----+------------+---------+--------+
| id | listing_id | user_id | amount |
+----+------------+---------+--------+
| 1 | 1 | 1 | 5 |
| 2 | 2 | 1 | 15 |
+----+------------+---------+--------+
Amount 15 is correct, but ID=2 is from the record with amount=10. How do I get the correct result with record id=1 and id=3?