计数值出现在MySQL的特定列中

时间:2022-08-17 15:43:42

This has probably been asked before, but I'm unable to make my way through the myriad of search results.

这可能是之前被问过的,但是我无法通过无数的搜索结果。

Given a non-normalized MySQL table, what is the most optimized query to count the number of times each distinct value of column x was used?

给定一个非规范化的MySQL表,什么是最优化的查询来计算列x的每个不同值的使用次数?

e.g. Given a table containing

例如给出一个包含的表

mike
mary
mike

Return results like:

返回结果如:

mike 2
mary 1

From the MySQL documentation, it would seem that count is an aggregate function that can be used with GROUP BY, but it's not doing what I want (it's returning the total number of rows in the GROUP BY, not the number of appearances for each row. i.e. this does not work SELECT count(email) as c FROM orders GROUP BY email

从MySQL文档中,看起来count是一个可以与GROUP BY一起使用的聚合函数,但是它没有按我想要的那样做(它返回GROUP BY中的总行数,而不是每行的出现次数) 。即这不起作用SELECT count(email)as c FROM orders GROUP BY email

4 个解决方案

#1


45  

select email, count(*) as c FROM orders GROUP BY email

#2


12  

SELECT column_name, COUNT(column_name)
FROM table_name
GROUP BY column_name

#3


1  

Take a look at the Group by function.

看一下按功能分组。

What the group by function does is pretuty much grouping the similar value for a given field. You can then show the number of number of time that this value was groupped using the COUNT function.

函数组的作用是对给定字段的类似值进行预分类。然后,您可以使用COUNT函数显示该值被分组的时间数。

MySQL Documentation

MySQL文档

You can also use the group by function with a good number of other function define by MySQL (see the above link).

您还可以使用具有MySQL定义的大量其他函数的group by函数(请参阅上面的链接)。

mysql> SELECT student_name, AVG(test_score)
    ->        FROM student
    ->        GROUP BY student_name;

#4


0  

select name, count(*) from table group by name;

i think should do it

我认为应该这样做

#1


45  

select email, count(*) as c FROM orders GROUP BY email

#2


12  

SELECT column_name, COUNT(column_name)
FROM table_name
GROUP BY column_name

#3


1  

Take a look at the Group by function.

看一下按功能分组。

What the group by function does is pretuty much grouping the similar value for a given field. You can then show the number of number of time that this value was groupped using the COUNT function.

函数组的作用是对给定字段的类似值进行预分类。然后,您可以使用COUNT函数显示该值被分组的时间数。

MySQL Documentation

MySQL文档

You can also use the group by function with a good number of other function define by MySQL (see the above link).

您还可以使用具有MySQL定义的大量其他函数的group by函数(请参阅上面的链接)。

mysql> SELECT student_name, AVG(test_score)
    ->        FROM student
    ->        GROUP BY student_name;

#4


0  

select name, count(*) from table group by name;

i think should do it

我认为应该这样做