Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

using SUM and GROUP BY when joining tables

$
0
0

Hi everyone,

 

I'm trying to give my students points for doing various things on my website. Eg: Completing a quiz, getting a certain score, all result in points, which are thrown into a table called points.

 

Now I'm trying to extract a leaderboard for the table.

 

I'm halfway there, and have a list of scores ranked from highest to lowest, next to the member's id.

 

See code below:

 

$query = "SELECT point_member_id, SUM(points) FROM tbl_points GROUP BY point_member_id ORDER BY SUM(points) DESC"; 
	 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
	echo  $row['point_member_id']. " : ". $row['SUM(points)'];
	echo "<br />";
}
	  
	

 

However, I really don't want point_member_id displaying. I'd like the student's firstname and lastname displaying.

 

firstname and lastname are the fields 'member_firstname' and 'member_lastname' in the table 'members'.

 

So what I need to do is extract this information where members.member_id = points.point_member_id

 

I've tried a bunch of variations to the query and searched ad nauseum, but nothing is helping.

 

Any advice would be greatly appreciated.

 

Cheers,

 

Dave

 

 

 


Viewing all articles
Browse latest Browse all 13200

Trending Articles