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我想统计一下表中"answer“字段的每个值在响应中的出现次数
我试过了,但没有得到
SELECT count(answer) as answer_cnt
FROM `sg_finished_surveys`
WHERE resopnse = $q GROUP BY `answer`其中$q等于唯一响应值。
发布于 2014-11-22 18:31:45
您希望使用count和group by语句来获取每种类型答案的数量:
SELECT
count(*) as answer_cnt,
`answer`
FROM
`sg_finished_surveys`
WHERE
response = '$q'
GROUP BY
`answer`这将计算每个答案的实例数,并给出实际答案。
您的响应子句中也有一个拼写错误(resopnse != where )。
您可能还想查看我发布的这篇Question and Answer,它涵盖了这种类型的查询和更多内容。
发布于 2014-11-22 18:30:02
使用group by visitorID,
SELECT count(answer) as answer_cnt FROM `sg_finished_surveys`
WHERE resopnse = $q group by visiterID发布于 2014-11-22 18:31:53
$q需要用单引号引起来。
SELECT count(*) as answer_cnt
FROM `sg_finished_surveys`
WHERE resopnse = '$q' GROUP BY `answer`https://stackoverflow.com/questions/27076294
复制相似问题