计算值出现在 MySQL 中特定列的次数 [英] Count number of times value appears in particular column in MySQL
问题描述
这可能以前有人问过,但我无法通过无数的搜索结果.
This has probably been asked before, but I'm unable to make my way through the myriad of search results.
给定一个非规范化的 MySQL 表,计算列 x 的每个不同值的次数的最优化查询是什么?
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?
例如给定一个包含
mike
mary
mike
返回如下结果:
mike 2
mary 1
从 MySQL 文档来看,count
似乎是一个可以与 GROUP BY
一起使用的聚合函数,但它没有做我想要的(它返回GROUP BY
中的总行数,而不是每行出现的次数.即这不起作用 SELECT count(email) as c FROM orders GROUP BY email
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
推荐答案
select email, count(*) as c FROM orders GROUP BY email
这篇关于计算值出现在 MySQL 中特定列的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!