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

Cœur
  • 37,241
  • 25
  • 195
  • 267
IndiCoder
  • 1
  • 2

4 Answers4

1

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.

Community
  • 1
  • 1
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
0

use group by visitorID ie,

 SELECT count(answer) as answer_cnt FROM `sg_finished_surveys`
 WHERE resopnse = $q group by visiterID
Rashid
  • 293
  • 4
  • 14
0

$q need to be in single quote.

SELECT count(*) as answer_cnt
FROM `sg_finished_surveys`
WHERE resopnse = '$q' GROUP BY `answer`
fortune
  • 3,361
  • 1
  • 20
  • 30
0
SELECT COUNT(answer)
FROM `sg_finished_surveys`
WHERE respondence = '".$q."'
GROUP BY answer
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46