oasisfleeting Posted May 13, 2009 Share Posted May 13, 2009 I was hoping to get some help on consolidating these three queries into only one query. Or perhaps someone could critique the code and maybe give me some pointers how I could improve it. $seql = mysql_query("SELECT DISTINCT `StudentsID` FROM tbl_points"); for($i=0;$i<$numStuds;$i++) { $res = mysql_fetch_array($seql); $q = mysql_query("SELECT StudentName FROM tbl_students WHERE StudentsID = " . $res['StudentsID']); $rs = mysql_fetch_array($q); $qu = mysql_query("SELECT SUM(SPointsNo) as totalPoints FROM tbl_points WHERE StudentsID = " . $res['StudentsID']); $resu = mysql_fetch_array($qu); ?> <tr> <td class="FormDesc"><?php echo $res['StudentsID']; ?></td> <td class="FormDesc"><?php echo $rs['StudentName']; ?></td> <td class="FormDesc"><?php echo $resu['totalPoints']; ?></td> </tr> <? } Quote Link to comment https://forums.phpfreaks.com/topic/158036-consolidating-three-queries-into-one/ Share on other sites More sharing options...
Adam Posted May 14, 2009 Share Posted May 14, 2009 $query = mysql_query(" SELECT SUM( `SpointsNo` ) totalPoints, `StudentsID` FROM tbl_points t1 INNER JOIN tbl_students t2 WHERE ( t1.`StudentsID` = t2.`StudentsID` ) GROUP BY `StudentsID` "); ... wild shot in the dark! EDIT: Or maybe.. $query = mysql_query(" SELECT SUM( `SpointsNo` ) totalPoints, `StudentsID` FROM tbl_points t1 INNER JOIN tbl_students ON t1.`StudentsID` = tbl_students.`StudentsID` GROUP BY `StudentsID` "); Hard to work out when you can't test the SQL, for me at least... Quote Link to comment https://forums.phpfreaks.com/topic/158036-consolidating-three-queries-into-one/#findComment-833993 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.