Jump to content

Possible to do a join also using COUNT(*) ?


suttercain

Recommended Posts

Hi everyone, I am currently using the following code:

 

<?php
$stats = mysql_query("SELECT * FROM totalCalls WHERE operatorId = '".$operatorId."'") or die(mysql_error());
$morestats = mysql_query("SELECT typeId, COUNT(*) as numcalls FROM totalCalls WHERE operatorId = '".$operatorId."' GROUP BY typeId") or die(mysql_error());
$res = mysql_query("SELECT * FROM typeOfCalls ORDER BY typeId") or die(mysql_error());
$total = mysql_num_rows($stats);
echo "<div id=\"rightborder\">";
echo "<div class=\"right\">";
echo "<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo "Total Calls Answered Today: $total";
echo "<br />";
while ($row1 = mysql_fetch_assoc($res)) {
while ($row = mysql_fetch_assoc($morestats)) {
$rowz = $row["numcalls"];
echo"<tr>";
echo "<td>" .$row1['type']. "</td>";
echo "<td>$rowz</td>";
echo "</tr>";
}
}
echo "</table>";
echo "</div></div>";
?>

 

I am just starting to use joins and have had sucess. But now I am trying to use th COUNT(*) syntax in the MySQL Statement... but could not get it to work. When I tried it, it was counting both tables and I need it to only count typeId. I got it working... somewhat using the code above with a nested while loop but it  echoes the "$row['type']"

like this:

 

Duel Sport

Duel Sport

Duel Sport

 

Instead of...

Duel Sport

Single Sport

Sports

 

Is it possible to combine the two SELECT statement from my code above and have it function, only using the COUNT on one table?

 

If so, what is the proper syntax? Thanks guys.

 

SC

Link to comment
https://forums.phpfreaks.com/topic/54804-possible-to-do-a-join-also-using-count/
Share on other sites

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.