Php 计算mysql中的出现次数
我想计算表中“应答”字段的每个值相对于响应的出现次数 我试过了,但没有成功Php 计算mysql中的出现次数,php,mysql,sql,Php,Mysql,Sql,我想计算表中“应答”字段的每个值相对于响应的出现次数 我试过了,但没有成功 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
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
其中,
$q
等于唯一响应值。按visitorID使用分组
即
您希望使用count和group by语句获取每种类型答案的编号:
SELECT count(answer) as answer_cnt FROM `sg_finished_surveys`
WHERE resopnse = $q group by visiterID
这将计算每个答案的实例数,并给出实际答案
您的where
子句(resopnse!=response)中也有输入错误
您可能还想查看我发布的这篇文章,它涵盖了这类查询以及更多内容。
$q
需要包含在单个报价中
SELECT
count(*) as answer_cnt,
`answer`
FROM
`sg_finished_surveys`
WHERE
response = '$q'
GROUP BY
`answer`
否,我想计算您在代码中编写的“resopnse”中的答案列中出现的1,2,3,4,5,而不是“response”,使用visitorIDValid组的答案值似乎包括0,1,2和3。那么4和5是从哪里来的呢?
SELECT
count(*) as answer_cnt,
`answer`
FROM
`sg_finished_surveys`
WHERE
response = '$q'
GROUP BY
`answer`
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