计算 MySQL 中的出现次数


count occurrences in mysql

resp_id visitorID surveyID questionID response answer userID
     43       777      163        736 MS            0      1
     42       777      163        736 Rohit         1      1
     41       777      163        736 Virat         1      1
     40       776      163        736 MS            1      1
     39       776      163        736 Rohit         3      1
     38       776      163        736 Virat         1      1
     37       775      163        736 MS            0      1 
     36       775      163        736 Rohit         1      1 
     35       775      163        736 Virat         2      1
     34       774      163        736 MS            2      1
     33       774      163        736 Rohit         3      1
     32       774      163        736 Virat         1      1

我想计算表中"答案"字段的每个值相对于响应的出现

次数

我试过但没有得到

SELECT count(answer) as answer_cnt
FROM `sg_finished_surveys`
WHERE resopnse = $q GROUP BY `answer`

其中$q等于唯一响应值。

您希望使用计数和分组依据语句来获取每种答案的类型:

SELECT 
    count(*) as answer_cnt,
    `answer`
FROM 
    `sg_finished_surveys`
WHERE 
    response = '$q'
GROUP BY 
    `answer`

这将计算每个答案的实例数,并为您提供实际答案。

您的where子句中还有一个错别字(resopnse != 响应)。

您可能还想查看我发布的此问题和答案,其中涵盖了此类查询以及更多内容。

按访客 ID 使用组即,

 SELECT count(answer) as answer_cnt FROM `sg_finished_surveys`
 WHERE resopnse = $q group by visiterID

$q需要用单引号引起来。

SELECT count(*) as answer_cnt
FROM `sg_finished_surveys`
WHERE resopnse = '$q' GROUP BY `answer`
SELECT COUNT(answer)
FROM `sg_finished_surveys`
WHERE respondence = '".$q."'
GROUP BY answer