Poll with PHP

Asked

Viewed 59 times

-2

10 users will answer a poll with 19 options equal to all, each answer is saved in the database in a table called 'votes' with the following structure: id: 1 - user_id: 1 - answer: {user response}

I want to count all 10 responses and show the most voted, for example. 3 people voted 'CASA'. 5 people voted 'WORK' option and 2 people voted 'LEISURE'.

How can I show that the most voted was WORK and that 5 people voted for her?

  • if the answer field is varchar-like you may have problems, but I believe all you need to do is a Count in the response field and group by user.

1 answer

1


If I understand your question correctly ...

and if the table is like this .....

tabela

then select is this

Select resposta, count(resposta)
from votos
group by resposta
having count(resposta)=(Select max(A.CNT)
from (Select count(resposta) as CNT
from votos
group by (resposta)) as A)

and the result is this

resultado

  • It works. But I didn’t understand anything kkkkkk.

  • @Vitorleite, because it is, has to give a researched/ studied in each query clause for you understand, Count, group, having. Now if the answer served mark it as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.