简体   繁体   English

计算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

I want to count occurrence of each value of "answer" field in table respect to response 我想对响应中的表中“答案”字段的每个值进行计数

I have tried but did not get 我已经尝试过但没有得到

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

Where $q is equal to unique response value. 其中$q等于唯一响应值。

You want to use a count and a group by statement to get the number of each type of answer: 您要使用一个计数和一个group by语句来获取每种答案的数量:

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

This will count the number of instances of each answer as well as giving you the actual answer. 这将计算每个答案的实例数量,并为您提供实际答案。

You also have a typo in your where clause (resopnse != response). 您的where子句中也有错字(resopnse!= response)。

You may also want to check out this Question and Answer that I posted which covers this type of query and a lot more. 您可能还需要检查一下我发布的“ 问答”,其中涵盖了此类查询以及更多内容。

use group by visitorID ie, 使用visitorID分组,即

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

$q need to be in single quote. $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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM