[英]MySQL return count of each element AND return all occurrences of that element
I can get a count of the number of times each element occurs using the following MySQL 我可以使用以下MySQL来获取每个元素出现次数的计数
SELECT COUNT(*) AS count
FROM example
GROUP BY example.value
The problem is, because of the GROUP BY statement, duplicate records will not be returned with their COUNT value. 问题是,由于有GROUP BY语句,重复的记录将不会返回其COUNT值。
IE I need this: IE浏览器,我需要这个:
But I get this: 但是我得到这个:
Any ideas how this could be done? 任何想法如何做到这一点? I am thinking some kind of a join, but I can't figure out the proper way to compare the table to itself 我正在考虑某种联接,但我不知道将表与其自身进行比较的正确方法
You can use JOIN
: 您可以使用JOIN
:
select a.*, b.cnt
from example a
join (
select value, count(*) cnt
from example
group by value
) b on a.value = b.value;
SELECT e.value, t.count
FROM example e
LEFT JOIN (SELECT value, COUNT(*) AS count
FROM example
GROUP BY example.value) t
ON e.value = t.value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.