gpt4 book ai didi

mysql - 计算某些内容在列中出现的次数并记录下来

转载 作者:行者123 更新时间:2023-11-29 12:26:51 25 4
gpt4 key购买 nike

因此,为了知道表中有多少人叫 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com