I have a table of bids for an auction system:
BIDS
id user_id bid auction_id
1 3 1 1
2 4 2 1
3 4 1 1
4 3 3 1
5 3 2 1
6 3 2 1
I need to get the highest user bid for a given user in a given auction (the sum of bids up to the last bid made by THAT user, taking into consideration that a user might not have made a bid at all).
For example:
for user id=3, highest bid would be 11.
for user id=4, highest bid would be 4.
for user id=6, highest bid would be 0. (user didn't make any bids)
So far I have this query:
SELECT COALESCE( SUM( bid ), 0 ) as highest_bid
FROM BIDS
WHERE user_id = 3
AND auction_id = 1
But is returning just the sum all all bids made by that user only, and I need the sum of bids up to the last bid made by THAT user.
I hope I make sense.
Thank you!