Hello.
I have a table with the following rows for example:
and I want to show the user what hes rank (in numbers).
The total power is the Strength+Speed+Power.
If there are 2 users:
user1: 20 strength, 10 speed , 10 power (sum: 40)
user2: 30 strength,15 speed, 15 power (sum:60)
it will show to user1: you rank is #2 .
How I can build such a query that does that and give me the count or orders by the sum so I can run a loop until it find the right user?
Thanks in advance!
r937
May 17, 2014, 11:30am
2
which database? because if it’s mysql you won’t be able to use window functions
Maybe it would be better to query the sum
table?
@theunreal ; Are you looking for a database or a PHP solution?
i.e. should this thread be moved to the PHP forum?
I run it with PHP but I need to know which query I should run, sothe solution releated to both of the forums :S
r937
May 18, 2014, 5:31pm
6
SELECT t.userid
, t.strength +
t.speed +
t.power AS superpower
, ( SELECT COUNT(*) + 1
FROM test_ranks AS z
WHERE z.strength + z.speed + z.power
> t.strength + t.speed + t.power ) AS rank
FROM test_ranks AS t
WHERE t.userid = 937
no loop required!!