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.
+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
2010-04-05 11:05:58
@Mark it's perfect.but what happened if number 4 has count zero? i get 4 in resultset with count zero or not?
Salil
2010-04-05 11:16:23
@Salil If there's no vote for a particular number it will not appear in the result set.
Petesh
2010-04-05 11:41:43
is it there any way so that i get default count =0 if no vote is present?
Salil
2010-04-05 12:05:21
A:
here you go: http://www.joedolson.com/poll-with-php-and-mysql.php It's simple poll
confiq
2010-04-05 11:06:42