Select the Nth result from SQL 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. Which are intigers approx 1/4 1/2 and 3/4 of way through the results.

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

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

"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. Which are intigers approx 1/4 1/2 and 3/4 of way through the results. "

Well, what identifies $N, $M, and $O? What makes them different, so you know you want them and not others? Obviously they aren’t random. If there is no way to test the unique parts in MySQL, there is certainly a way to do it in PHP. Just run your query and go through the loop in PHP, running the checks (to see if it is row $N, $M, or $O) on each one.


#3

It is 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.

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.


#4

Why not just set a var to mysql_num_results($query) and then select the $var * 1/2, $var * 3/4, etc? Your question isn’t quite clear to me, but if it is what I think it is, this is the best way of doing it.


#5

I have variables set to the right rows that i want, but the problem is How do i get the data. The only time I know what rows I want is if the colum is sorted by the statment. otherwise result N wont have the needed information.


#6

“How do i get the data”
Here would be a good place to start:
http://ca3.php.net/manual/en/function.mysql-result.php


#7

I have read that page 3 times over 2x before you posted the reply, but I still do not see how to get the data. I know that I want the Nth record selected by the query listed above How do I just select the information from the Nth record? If I do a standard query then It wont sort by the information needed and is no longer the nth record.