Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-按天计算的组比较计数_Mysql_Group By_Comparison - Fatal编程技术网

MySQL-按天计算的组比较计数

MySQL-按天计算的组比较计数,mysql,group-by,comparison,Mysql,Group By,Comparison,我有以下疑问: select DATE_FORMAT(created_at,'%d %M') as day, count(*) as Total, count(content_sentiment = 0) as Neutral, count(content_sentiment < 0) as Negative, count(content_sentiment > 0) as Positive from Master where content_

我有以下疑问:

select
    DATE_FORMAT(created_at,'%d %M') as day,
    count(*) as Total,
    count(content_sentiment = 0) as Neutral,
    count(content_sentiment < 0) as Negative,
    count(content_sentiment > 0) as Positive
from Master
where content_sentiment IS NOT NULL
GROUP BY DAY(created_at)
;
总值应该是正值、中性值和负值的总和,这不是猜测,因为每个表的查询都会得到整个表的总值,而不是按天分组


如何为每个正值、中性值和负值包含group by?

您需要条件聚合:

...
count(*) as Total,
SUM(CASE WHEN content_sentiment = 0 THEN 1 ELSE 0 END) as Neutral,
SUM(CASE WHEN content_sentiment < 0 THEN 1 ELSE 0 END) as Negative,
SUM(CASE WHEN content_sentiment > 0 THEN 1 ELSE 0 END) as Positive
...

返回SELECT语句检索到的行中expr的非空值数。谢谢,非常有用。
...
count(*) as Total,
SUM(CASE WHEN content_sentiment = 0 THEN 1 ELSE 0 END) as Neutral,
SUM(CASE WHEN content_sentiment < 0 THEN 1 ELSE 0 END) as Negative,
SUM(CASE WHEN content_sentiment > 0 THEN 1 ELSE 0 END) as Positive
...
    select
    DATE_FORMAT(created_at,'%d %M') as day,
    count(*) as Total,
    sum(if(content_sentiment = 0,1,0)) as Neutral,
    sum(if(content_sentiment < 0,1,0)) as Negative,
    sum(if(content_sentiment > 0,1,0)) as Positive
from Master
where content_sentiment IS NOT NULL
GROUP BY DAY(created_at)
;