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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.