My website allows users to record bids. Each bid is saved individually and associated to a user Id. A user can have many bids which are used to add up to one overall bid which is displayed upon the site.
What I am trying to do in sql is return the position a users overall bid is from a result set.
The sql I am using is below but problems arise when I use the group by command – the ordering seems to revert back to the default db order rather than by the sum of a users bid amounts:
SET @rowcount = 0;
SELECT rowCount, userId FROM (
SELECT userId, @rowcount := @rowcount + 1 as rowCount, sum(amount) as amount FROM bids group by userId order by amount desc
) t when product = xxxxx
appreciate if anyone knows if this is possible?
You need to move rowcount incrementation out of subquery. And put your
WHEREcondition inside, otherwise your subquery will sum bids on all products for a given user.