tags:

views:

123

answers:

3

I want to make a little php poll. The script should ask the users a question they can only answer by numbers from 0-999. After pressing the submit button the data should be stored into mysql. So I just want to know how much users choosed the same number (in percent). It's a simple poll but I don't want any output to be shown.

A: 

Freelancer.com and Elance.com let you hire developers to do work.

Coronatus
+2  A: 

You need to use COUNT and GROUP BY:

SELECT
    number,
    COUNT(number) * 100 / (SELECT COUNT(*) FROM table1) AS percent
FROM table1
GROUP BY number
ORDER BY COUNT(number) DESC

Results:

number  percent
2       50.0000
3       30.0000
1       20.0000

Test data:

CREATE TABLE table1 (number INT NOT NULL);
INSERT INTO table1 (number) VALUES (1),(1),(2),(2),(2),(2),(2),(3),(3),(3);
Mark Byers
@Mark it's perfect.but what happened if number 4 has count zero? i get 4 in resultset with count zero or not?
Salil
@Salil If there's no vote for a particular number it will not appear in the result set.
Petesh
is it there any way so that i get default count =0 if no vote is present?
Salil
A: 

here you go: http://www.joedolson.com/poll-with-php-and-mysql.php It's simple poll

confiq