Jump to content

Consolidating three queries into one.


oasisfleeting

Recommended Posts

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>
	<?
}

Link to comment
https://forums.phpfreaks.com/topic/158036-consolidating-three-queries-into-one/
Share on other sites

$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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.