Jump to content

Poll Results - Percentage


Manny

Recommended Posts

I have a database table with the following information

poll_id | option_id | option_name | option_votes---------------------------------------------------------1	   | 1		 | option_1	| 31	   | 2		 | option_2	| 21	   | 3		 | option_3	| 61	   | 4		 | option_4	| 781	   | 5		  | option_5	| 651	   | 6		  | option_6	| 11	   | 7		  | option_7	| 31	   | 8		 | option_8	| 51	   | 9		 | option_9	| 121	   | 10		| option_10	| 8

The 'option_votes' column shows how many votes each option has from in the poll. What I want to be able to do is return these results as a percentage.I have been trying the following query:

SELECT *, FORMAT((`option_votes`/SUM(`option_votes`)*100),2) AS `total_votes` FROM `poll_options` WHERE `poll_id` = '1'

However, this only returns the first row of the table and I want each row to display, so long as the 'poll_id' is equal to 1 (more polls will follow, with each poll taking it's own id).I have read elsewhere that you can solve the problem with 'GROUP BY', but this doesn't seem to be working either. Any help that can be provided will be very much appreciated.

Link to comment
Share on other sites

You could group by option_id to return all rows, but that's going to give 100% for each row also. I think you need to return what you have in your table, do another query to get the total votes for that option, and do the math in PHP or whatever language you're using. I'm sure there's a way to do that with a single query but I think you're getting into temporary tables and things like that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...