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
- Query all students from the database
- Then for EACH student, Query the result from the database
- This way, if we have 100 students, we need to query the database 100 times.
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
- Query all student from the database
- 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,...)
- Then using foreach statement, process the result as required.
- 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
精彩评论