Select Nth Row Of a Sorted Query


#1

Ok I have the following query for MySQl

"SELECT * FROM `{$survey_answers}` ORDER BY 'Q{$i}-RND{$RND}' ASC";

The query works fine and I can do a loop to get all results, but i dont want all results I just need row $N, $M, and $O for statistical annalysis. I have no way of knowing what the row is until the table is sorted by the variable listed above, Once it is sorted I want the values for that variable Numrows/4 numrows/2 and numrows*3/4ths These then use the PHP round command to get ints and if that int is less then 1 I set it to 1.

Please note Table Not Indexed by ‘Q{$i}-RND{$RND}’ which = somthing like Q1-RND1 … etc

I am well aware that I can do a while loop and use an if statement to grab those 3 values, but the reason I posted this is I am sure there has to be a better way of doing that.

I Should note that that solution works in a small data set, but since that Loop has to run for each question sometimes 100+, and then In that case since it would be looping through each user it could easily mean doing 100 loops that are 2000(Num of users) intervals just to get 300 peices of data.

More server load then desired if it didnt time out.

Thanks In advance. I am sure it is a simple command that I am missing, but for the life of me I cannot find it in the online manuel for PHP or MYSQL.


#2

I beleive the SQL LIMIT keyword lets you set a starting point ( http://dev.mysql.com/doc/mysql/en/SELECT.html )

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must be integer constants. With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):