简体   繁体   中英

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.

You want to use a count and a group by statement to get the number of each type of answer:

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).

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,

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

$q need to be in single quote.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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