如何返回不同的值及其计数? [英] How to return distinct values and their count?

查看:133
本文介绍了如何返回不同的值及其计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望做的是(希望)很简单,但是我只是语法不正确.我想返回表中所有不同的值,并计算每个值有多少条记录.

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.

因此,在PHP中,我得到了:

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子句来获取它:

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

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

这篇关于如何返回不同的值及其计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆