基本SQL COUNT查询

时间:2018-04-23 04:47:49

标签: sql count

我有一个数据库

f_name  l_name  post_code
John    Smith   3156
Sean    Jones   3156
Steve   Black   3114
michael Lever   3156

我想为每个具有相同邮政编码的行创建一个计数。 例如:

post_code    Count
3156         3 
3114         1

我怎样才能做到这一点?

5 个答案:

答案 0 :(得分:2)

只需group by

select post_code, count(*) as Count
from table t
group by post_code;

答案 1 :(得分:1)

我认为你在寻找:

select 
  post_code,
  count(distinct f_name, l_name)
from
  DATABASE
group by
  post_code

我还建议您为变量计数命名,以便以后更容易找到它。

答案 2 :(得分:1)

简单的查询。

使用 GROUP BY post_code 赞。

select post_code, count(*) as totalcount
from table abc
group by post_code;

答案 3 :(得分:0)

您可以在此处使用 GROUP BY

GROUP BY - GROUP BY语句通常与聚合函数(COUNT,MAX,MIN,SUM,AVG)一起使用,以将结果集分组为一列或多列。

<强>查询

select post_code, count(*) as totalcount from table table1 group by post_code;

答案 4 :(得分:0)

我们假设您的表名是T1。使用以下查询来获得预期的输出:(请注意,在我发布此答案之前,您不能使用在其他答案中使用的关键字“表格”)

select post_code, count(*) as Count
from T1
group by post_code;

有关GROUP BY的一些快速信息/理解,您可以参考:https://www.w3schools.com/sql/sql_groupby.asp

相关问题