作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,为了知道表中有多少人叫 Johnny,我需要执行以下查询。
查询:
Select count(*) from mytable where first = 'Johnny';
结果是 2。
但是我想要做的是将这个数字记录在计数列中,以便最终结果像这样。
+--------+----------+
| First | COUNT |
+--------+----------+
| Johnny | 2 |
| Diane | 1 |
| Johnny | 2 |
| Harold | 1 |
| Amy | 3 |
| Roy | 2 |
| Amy | 3 |
| Amy | 3 |
| Roy | 2 |
+--------+----------+
是否有任何查询或过程能够产生这种类型的输出?
最佳答案
要获得准确的输出,您需要 use a subquery :
select
mytable.First,
counts.`COUNT`
from
mytable
join (
select
First,
count(*) `COUNT`
from
mytable
group by
First
) counts on mytable.First = counts.First;
关于mysql - 计算某些内容在列中出现的次数并记录下来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190842/
我是一名优秀的程序员,十分优秀!