I want to make a little php poll. The script should ask the users a question they can only ans开发者_运维知识库wer 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.
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);
here you go: http://code.tutsplus.com/articles/creating-a-web-poll-with-php--net-14257 It's simple tutorial how to make poll.
YOu can use http://www.phpkobo.com/ajax_poll.php if you need something done..
精彩评论