开发者

Which Method is better to display large result from database?

Few days back, I posted a question about getting student marks results from database with least number of queries. What I need to know is which of the following approach is better?

Case A

  1. Query all students from the database
  2. Then for EACH student, Query the result from the database
  3. This way, if we have 100 students, we need to query the database 100 times.
  4. Example:

    $Students = mysql_query(SELECT student_id, student_name FROM STUDENTS WHERE class_id = 2);
    
    while($student = mysql_fetch_assoc($Students) {
        $Result = mysql_query("SELECT marks FROM result WHERE student_id = ".$student['student_id']);
        // Process result
    
    }
    

Case B

  1. Query all student from the database
  2. Then query result of above students using "IN" operator like SELECT marks FROM result WHERE student_id IN (23,34,23,343,25,67,65,...)
  3. Then using foreach statement, process the result as required.
  4. This way, it we have 100 students, we need to qu开发者_StackOverflow社区ery the database just 2 times.

I need to know which of the above two cases is better?

Thanks


select * from students join marks on students.student_id = marks.student_id

This should do it. And ofcourse the second option of yours seems to be better.


wouldn't it be:

SELECT marks FROM result JOIN students ON students.student_id = result.student_id;

join reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜