gpt4 book ai didi

mysql - 无条件计算 1 列 2

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:07 24 4
gpt4 key购买 nike

我想统计一栏的内容,没问题。但我也想计算另一列内容,但有 where 条件。

为了更好地解释,我将其可视化:

我的 table

Index1       Index2
1 0
1 2
2 2
2 2
2 2

现在我想计算 Index1 的内容,我知道数字 1 和 2 出现的频率。但进一步我想计算 Index2 的内容,但只计算大于 1 的数字。

结果

Index1       amount
1 1
2 3

但是当我使用 where 条件时,它用于两列。我用 distinct 试过,但没用。

这个问题是否有一些舒适的解决方案?

最佳答案

这称为条件聚合,在 MySQL 中它非常简单:

SELECT t.index1,
COUNT(*) as amount
SUM(t.index2>1) as amountWithWhere
FROM YourTable t
GROUP BY t.index1

关于mysql - 无条件计算 1 列 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46222152/

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