Link to home
Start Free TrialLog in
Avatar of ScotTFO
ScotTFOFlag for United States of America

asked on

3 column count in PHP

My database is setup like this
Type - Identifier - Name

Currently I am displaying the Type and the count of how many times it appears.  I would like to show next to it the name that has the most occurrences of the type in the database.

This is the code I am using
echo "<table border=1><tr><td><strong>Type</strong></td><td><strong>Count</strong></td></tr>";
$query =  mysql_query("SELECT COUNT(*) as cnt, type FROM db GROUP BY type ORDER BY cnt DESC");

while ($row = mysql_fetch_array($query)) {
	echo "<tr><td>" . $row['type'] . "</td><td>" . $row['cnt'] . "</td></tr>";

echo "</table>";

Open in new window

How can I have the sql query count which Name has the most occurrences of type in the above code?

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Uhh, would you want to add the column name for the "name" to the query?  Then echo $row["name"] or something like that?
Avatar of ScotTFO


Yeah..just trying to add a 3rd column but not sure how to do a second count within that SQL query.
Avatar of Chris Harte
Sounds like the function you require is array_count_values
Please explain this a little more.  What exactly do you want to count?  If you could give us an example it might be helpful.  Thanks, ~Ray
Avatar of wwwdeveloper2

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial