Red Red - 12 days ago 10
MySQL Question

SQL Query to count number of rows searched until condition is met

Just as the title says, I need a query to count the rows until the condition is met. Here's my setup:

partnumber

b

e

d

a

c

So if I'm going to search for partnumber = d it will sort the part number and return 4 (since d is the 4th when you sort the partnumber).

I can do this inside a loop. I'm just wondering if there is a query for this.

Thanks in advanced...

Answer
SELECT COUNT(*) FROM THE_TABLE WHERE partnumber <= (SELECT ID FROM THE_TABLE WHERE partnumber <= 'd');