I have an auction system where multiple users can bid, but I'm having a problem when bids occur at the same time as they are entered in the database with the same value in the bidding_price
column, eg:
0.02
0.02
0.02
I've mounted a select that returns all bids with equal values (duplicity) for a given auction:
SELECT bidding_price, count(*)
FROM bid_account
WHERE auction_id = 1335
GROUP BY bidding_price
Having Count(*) > 1
In that case he will return the duplicities for the auction with ID 1335.
The structure of the bid_account
table looks like this:
id | int(255) - index - primary - AUTO INCREMENT
auction_id | int(11) - id of auction
bidding_price | float(20,2) - bid amount, eg. 0.02
I even created a DELETE that can erase the duplicates, but it would be unfair to some user to have the bid erased due to a system error. Follow:
DELETE ba
FROM bid_account ba JOIN
(SELECT ba2.auction_id, ba2.bidding_price, MAX(ba2.id) as max_id
FROM bid_account ba2
WHERE ba2.auction_id = ?
GROUP BY ba2.auction_id, ba2.bidding_price
) ba2
ON ba2.auction_id = ba.auction_id AND
ba2.bidding_price = ba.bidding_price AND
ba2.max_id > ba.id
WHERE ba.auction_id = ?;
Is with "?
" there because it receives the value by a parameter.
Does anyone have any idea how I can solve this problem, avoiding the equal value in the bidding_price
table, but for a given auction identified by the auction_id
column?
In the table there are several equal values in bidding_price
, but what can not occur are equal values for the same auction, that is, for the same value in auction_id
.
I thought of creating an update, which checks beforehand if there are duplicates, if it exists it updates the values by inserting 1 cent more in each duplicity. But I think it would go away from the problem when there were 3 bids at the same time.
This SQL command will run on node, and will run in a cron every second, that is, every time it is performing this check. If there is a duplicate bid now, the next second it will identify and fix.
The bidding system works in conjunction with a 15-second countdown timer, and when someone makes a bid the timer returns to its initial count of 15 seconds.