Running the following query
SET @rownum := 0;
SELECT customer_id, @rownum := @rownum +1 AS rank, score
FROM game_table_customers gtc
INNER JOIN customers c ON c.customers_id = gtc.customer_id
INNER JOIN game_table gt ON gtc.table_id = gt.table_id
WHERE c.my_team =11095
AND gt.event_id =21110
ORDER BY score DESC
LIMIT 0 , 30
The correct result should look like this
CustID Rank Score
2 1 130000
39 2 99426
84 3 99178
259 4 98963
339 5 97796
However, what I am getting is the following
CustID Rank Score
2 2 130000
39 11 99426
84 20 99178
259 54 98963
339 69 97796
When I exclude the event_id clause I get the correct result. However when the event_id is included it skews it. I have tried only the event_id (removing the my_team clause) and same incorrect result.
Any ideas/suggestions as to why the result may be off would be much appreciated
I guess this has to do with the execution plan selected in every case and the mix with applying of row numbers – in some of those plans. The row numbers are calculated and then the
WHEREconditions are evaluated, so you see non-consecutive row numbers.Try this (first getting the 30 results you need, then applying row numbers):
I can’t test it now but you can also try this: