gpt4 book ai didi

mysql - 如何根据等于值的字符串属性的计数进行分组?

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:42 26 4
gpt4 key购买 nike

我有一张 table ,我已经设法将它缩小到这个范围:

Event        | Response
------------ | ------
Birthday | Yes
Anniversary | No
Anniversary | No
Birthday | Yes
Birthday | Yes

我想计算每个事件的"is"回复的数量,因此最终产品应该是:

Event        | numYes
------------ | ------
Birthday | 3
Anniversary | 0

我想它使用 group by EventResponse = 'yes' 的聚合器,但我不确定如何让它工作

最佳答案

试试这个:

select event, sum(Response = 'Yes') numYes
from your_table
group by event;

它利用了 MySQL 中 true 为 1 和 false 为 0 的事实,并导致更短的表达式。

实现相同目的的另一种方法是像这样使用 COUNT:

select event, count(case when Response = 'Yes' then 1 end) numYes
from your_table
group by event;

关于mysql - 如何根据等于值的字符串属性的计数进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42031537/

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