I have the following table, called sales
+--------+--------+
| fruit | amount |
+--------+--------+
| cherry | 124 |
| plum | 23 |
| pear | 19 |
| apple | 13 |
| banana | 4 |
| orange | 2 |
+--------+--------+
And using this to get the rank
SET @rank=0;
SELECT @rank:=@rank+1 AS rank, fruit, amount FROM sales ORDER BY amount DESC;
.
+------+--------+--------+
| rank | fruit | amount |
+------+--------+--------+
| 1 | cherry | 124 |
| 2 | plum | 23 |
| 3 | pear | 19 |
| 4 | apple | 13 |
| 5 | banana | 4 |
| 6 | orange | 2 |
+------+--------+--------+
So now I want to get the rank of apple
but I couldn't figure out how exactly to do it.
Best Answer
You can place your query inside a subquery and do a WHERE against it
PROPOSED QUERY
SAMPLE DATA
SAMPLE CODE EXECUTED
PROPOSED QUERY EXECUTED
GIVE IT A TRY !!!