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>");
您应该能够使用GROUP BY
子句获取它:
SELECT tagName, count(tagName) AS tagCount FROM tagTable GROUP BY tagName
SELECT tagName, count(*) as TagCount
FROM tagTable
group by tagname
SELECT DISTINCT tagName, COUNT(*) FROM tagTable GROUP BY tagName
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.