This has been doing my head in for hours now. It seems so simple but i just cant find the answers.
Basically I have a MYSQL table of the following example data, NOTE there are other columns but are not necessary for this question.
id | x | y 1 | 50 | 3 2 | 40 | 1 3 | 50 | 0 4 | 50 | 1 5 | 40 | 2
I want to order the table by x DESC and secondly y DESC. So I have this:
SELECT id, x, y FROM table_name ORDER BY x DESC, y DESC
Hopefully resulting in this:
id | x | y 1 | 50 | 3 4 | 50 | 1 3 | 50 | 0 5 | 40 | 2 2 | 40 | 1
Then given a specific id stored in a variable, I want to be able to find what row number its found in given the query. ie Find where it is ranked amongst the other rows.
I know there is a RANK and DENSE_RANK, if I need to use either I need RANK. But if using that's not the best solution, or how to use RANK please help.
For the above data if my id variable $id = 4, it's rank would be 2 as it is sorted second.
And i need this value returned as a variable. Thanks