Let’s suppose we have a table with the fallowing values introduced in this particular order: 10, 11, 7, 5, 17, 147, 555.
If I were to try to find out where 11 for example was situated if I would have ordered the table ascending I would see that the index is 4.
However for large amounts of data I would not like to get the whole table and sort it in php beacuse that would be stupid.
What I want to know is if mysql can return the index of a value in an ordered query without querying all the values.
Thank you!
UPDATE #1
The query that I wanted looks like this (thanks to bluefeet):
SELECT rownum FROM (
SELECT col1, @rownum:=@rownum+1 AS rownum
FROM yourtable,
(SELECT @rownum:=0) table1
ORDER BY col1) table2
WHERE col1=11
and you can test it here http://sqlfiddle.com/#!2/a21c8/12
MySQL doesn’t have a
rownumper se but you can simulate it the following way:see SQL Fiddle with Demo
Using this you can determine the
rownumberwhich might give you want you need.If you then want a specific
rownumberyou can wrap this in a subquery:see SQL Fiddle with demo