You must evaluate everybody's rank and select the Username last
SET @rnk=0; SET @rank=0; SET @curscore=0;
SELECT score,Username,rank FROM
(
SELECT AA.*,BB.Username,
(@rnk:=@rnk+1) rnk,
(@rank:=IF(@curscore=score,@rank,@rnk)) rank,
(@curscore:=score) newscore
FROM
(
SELECT * FROM
(
SELECT COUNT(1) scorecount,rating score
FROM mytable GROUP BY rating
) AAA
ORDER BY score DESC
) AA LEFT JOIN scores BB USING (score)
) A WHERE Username='jkz101022';
If you want the original row, then do this
SET @rnk=0; SET @rank=0; SET @curscore=0;
SELECT HEX(B.UUID) as UUID,B.Username,B.Rating,A.rank FROM
(
SELECT AA.*,BB.Username,
(@rnk:=@rnk+1) rnk,
(@rank:=IF(@curscore=score,@rank,@rnk)) rank,
(@curscore:=score) newscore
FROM
(
SELECT * FROM
(
SELECT COUNT(1) scorecount,rating score
FROM mytable GROUP BY rating
) AAA
ORDER BY score DESC
) AA LEFT JOIN scores BB USING (score)
) A INNER JOIN mytable B USING (Username)
WHERE A.Username='jkz101022';
UPDATE 2014-08-25 16:57 EDT
I Fixed My Code
SET @rnk=0; SET @rank=0; SET @curscore=0;
SELECT HEX(B.UUID) as UUID,B.Username,B.Rating,A.rank FROM
(
SELECT AA.*,BB.Username,
(@rnk:=@rnk+1) rnk,
(@rank:=IF(@curscore=rating,@rank,@rnk)) rank,
(@curscore:=rating) newscore
FROM
(
SELECT * FROM
(
SELECT DISTINCT Rating score FROM survivalgamesstats
) AAA
ORDER BY score DESC
) AA LEFT JOIN survivalgamesstats BB ON AA.score=BB.Rating
) A INNER JOIN survivalgamesstats B USING (Username)
WHERE A.Username='jkz101022';
I got this outout
mysql> SET @rnk=0; SET @rank=0; SET @curscore=0;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT HEX(B.UUID) as UUID,B.Username,B.Rating,A.rank FROM
-> (
-> SELECT AA.*,BB.Username,
-> (@rnk:=@rnk+1) rnk,
-> (@rank:=IF(@curscore=rating,@rank,@rnk)) rank,
-> (@curscore:=rating) newscore
-> FROM
-> (
-> SELECT * FROM
-> (
-> SELECT DISTINCT Rating score FROM survivalgamesstats
-> ) AAA
-> ORDER BY score DESC
-> ) AA LEFT JOIN survivalgamesstats BB ON AA.score=BB.Rating
-> ) A INNER JOIN survivalgamesstats B USING (Username)
-> WHERE A.Username='jkz101022';
+----------------------------------+-----------+--------+------+
| UUID | Username | Rating | rank |
+----------------------------------+-----------+--------+------+
| 000597056E564BF587DAA1D31E51E29F | jkz101022 | 1600 | 2 |
+----------------------------------+-----------+--------+------+
1 row in set (0.00 sec)
mysql>
UPDATE 2014-08-25 17:15 EDT
I tried it with rob160502
and got this:
mysql> SET @rnk=0; SET @rank=0; SET @curscore=0;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT HEX(B.UUID) as UUID,B.Username,B.Rating,A.rank FROM
-> (
-> SELECT AA.*,BB.Username,
-> (@rnk:=@rnk+1) rnk,
-> (@rank:=IF(@curscore=rating,@rank,@rnk)) rank,
-> (@curscore:=rating) newscore
-> FROM
-> (
-> SELECT * FROM
-> (
-> SELECT DISTINCT Rating score FROM survivalgamesstats
-> ) AAA
-> ORDER BY score DESC
-> ) AA LEFT JOIN survivalgamesstats BB ON AA.score=BB.Rating
-> ) A INNER JOIN survivalgamesstats B USING (Username)
-> WHERE A.Username='rob160502';
+----------------------------------+-----------+--------+------+
| UUID | Username | Rating | rank |
+----------------------------------+-----------+--------+------+
| 00022E3531F44C36AE058B3CF063C02F | rob160502 | 1650 | 1 |
+----------------------------------+-----------+--------+------+
1 row in set (0.00 sec)
mysql>
UPDATE 2014-08-25 17:38 EDT
I even tried HauntedCorpse
and got this:
mysql> SET @rnk=0; SET @rank=0; SET @curscore=0;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT HEX(B.UUID) as UUID,B.Username,B.Rating,A.rank FROM
-> (
-> SELECT AA.*,BB.Username,
-> (@rnk:=@rnk+1) rnk,
-> (@rank:=IF(@curscore=rating,@rank,@rnk)) rank,
-> (@curscore:=rating) newscore
-> FROM
-> (
-> SELECT * FROM
-> (
-> SELECT DISTINCT Rating score FROM survivalgamesstats
-> ) AAA
-> ORDER BY score DESC
-> ) AA LEFT JOIN survivalgamesstats BB ON AA.score=BB.Rating
-> ) A INNER JOIN survivalgamesstats B USING (Username)
-> WHERE A.Username='HauntedCorpse';
+----------------------------------+---------------+--------+------+
| UUID | Username | Rating | rank |
+----------------------------------+---------------+--------+------+
| 000399B665D14979B3C0AF309112625F | HauntedCorpse | 1536 | 5 |
+----------------------------------+---------------+--------+------+
1 row in set (0.00 sec)
mysql>
Give it a Try !!!
CAVEAT : Please note that the results come from your actual data in SQLFiddle. I simply loaded the data into a test database and ran my code against it.
HEX(UUID) as UUID, Username, Rating
followed by all ranks that share that same rating (like 1600 (1,2), 1536 (3), 1234 (4), 455 (5)) or do you just want the rank ofjkz101022
(455) ?