How to return distinct values and their count?

What I'm trying to do is (hopefully) simple, but I just don't quite have the right syntax. I'd like to return all distinct values in a table with a count of how many records for each value.

So, in PHP, I've got:

$result = mysql_query("SELECT DISTINCT tagName FROM tagTable");
while($row = mysql_fetch_array($result)){
    echo("<p>" . $row['tagName']) . "</p>");
}

This works well. The distinct values are returned! But now how do I get each distinct value's count to display as well? I would want something to the effect of:

echo("<p>" . $row['tagName']) . $tagCountGoesHere . "</p>");

Answers


You should be able to get that using the GROUP BY clause:

SELECT tagName, count(tagName) AS tagCount FROM tagTable GROUP BY tagName

Need Your Help

Reset state of button

jquery css button

on my website I have 4 css styled buttons in the header of a table. When each button is clicked it currently changes the css correctly as per the below script (the below code may not be the easies...

Path to layout xml files in magento

layout magento-1.7

I have seen that we can place our layout xml files in

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.