我有一张数据表:
BID USER RETRACTED
1500 75 0
1900 75 0
2000 75 0
2000 75 0
2500 586 0
2750 75 0
3000 75 0
3250 1010 0
3500 75 0
3750 1010 0
4000 75 0
这是一个竞价平台,但我遇到的问题是用户可以撤消他们的出价。如果他们这样做,则需要修改DB中的信息。出价按增量排列。
目前我已经知道,如果USER 1010撤回其出价,那么当我实际需要它为2750时,最高出价仍为4000,因为这是2个用户之间的最高值。
答案 0 :(得分:2)
这部分最简单:
这样的事情:
DELETE FROM bids WHERE user=1010;
SET @highbidder=(SELECT user FROM bids ORDER BY bid DESC LIMIT 1;
SET @secondbid=(SELECT bid FROM bids WHERE user != @highbidder ORDER BY bid DESC LIMIT 1);
SET @newhigh=(SELECT bid FROM bids WHERE bid > @secondbid ORDER BY bid ASC LIMIT 1)
DELETE FROM bids WHERE bid > @newhigh;
可以压缩三个SET
语句:
SET @newhigh=(SELECT bid FROM bids WHERE bid > (SELECT bid FROM bids WHERE user != (SELECT user FROM bids ORDER BY bid DESC LIMIT 1) ORDER BY bid DESC LIMIT 1) ORDER BY bid ASC LIMIT 1);
然而,我不认为这是一个好主意,部分原因是出于性能原因,但主要是为了可读性。