Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
如果使用GROUP BY,则MySQL计数_Mysql_Count - Fatal编程技术网

如果使用GROUP BY,则MySQL计数

如果使用GROUP BY,则MySQL计数,mysql,count,Mysql,Count,我正在尝试解决LeetCode问题(): 我已在本地生成了调查日志: mysql> select * from survey_log; +------+--------+-------------+-----------+-------+-----------+ | uid | action | question_id | answer_id | q_num | timestamp | +------+--------+-------------+-----------+-------

我正在尝试解决LeetCode问题():

我已在本地生成了
调查日志

mysql> select * from survey_log;
+------+--------+-------------+-----------+-------+-----------+
| uid  | action | question_id | answer_id | q_num | timestamp |
+------+--------+-------------+-----------+-------+-----------+
|    5 | show   |         285 |      NULL |     1 |       123 |
|    5 | answer |         285 |    124124 |     1 |       124 |
|    5 | show   |         369 |      NULL |     2 |       125 |
|    5 | skip   |         369 |      NULL |     2 |       126 |
+------+--------+-------------+-----------+-------+-----------+
我想使用这个辅助表:

mysql> select question_id, if(action='show', 1, 0) as is_show, if(action='answer', 1, 0) as is_answer from survey_log;
+-------------+---------+-----------+
| question_id | is_show | is_answer |
+-------------+---------+-----------+
|         285 |       1 |         0 |
|         285 |       0 |         1 |
|         369 |       1 |         0 |
|         369 |       0 |         0 |
+-------------+---------+-----------+
接下来,我要做的是获得每个
问题id
is\u show
is\u answer
列的总和。我认为这会起作用:

mysql> select question_id, count(if(action = 'show', 1, 0)) as show_count, count(if(action = 'answer', 1, 0)) as answer_count from survey_log group by question_id;
+-------------+------------+--------------+
| question_id | show_count | answer_count |
+-------------+------------+--------------+
|         285 |          2 |            2 |
|         369 |          2 |            2 |
+-------------+------------+--------------+
但是,这并没有给我预期的输出

+-------------+------------+--------------+
| question_id | show_count | answer_count |
+-------------+------------+--------------+
|         285 |          1 |            1 |
|         369 |          1 |            0 |
+-------------+------------+--------------+
最后一个查询有什么问题?我已经看过了,但似乎无法将其应用于这个问题。

只是计算非空值。因此
0
1
都将被计算在内。您需要做的是:

请注意,由于MySQL在数字上下文中将布尔值视为0(false)或1(true),因此可以删除查询的
if
部分:

select question_id, 
       sum(action = 'show') as show_count,
       sum(action = 'answer') as answer_count
from survey_log
group by question_id;
select question_id, 
       sum(action = 'show') as show_count,
       sum(action = 'answer') as answer_count
from survey_log
group by question_id;