Jump to content

Recommended Posts

I have created a table to track poll results, i would like to pull number of total votes, number of votes per answers id then get avg of votes. Used this sql:

 

SELECT p.question, a.answer, count( r.polls_id ) AS total, count( r.poll_answers_id ) AS answer_total FROM polls p, poll_answers a, poll_results r WHERE p.polls_id = a.polls_id AND p.polls_id = r.polls_id AND a.poll_answers_id = r.poll_answers_id AND p.polls_id =2 GROUP BY p.polls_id, r.poll_answers_id

 

Get these results:

Do you think the U.S. should or should not have a ...  Should Have            3    3

Do you think the U.S. should or should not have a ... Should Not Have 1 1

 

Obviously the numbers are not correct as i am getting both for total votes and total answers the same. How should this SQL be formated or do I need to do separate queries

 

Here are the table structures:

 

polls:

polls_id  int(10) unsigned    PRI  NULL  auto_increment

question text      

active tinyint(4)     0

 

poll_answers:

poll_answers_id  int(10) unsigned    PRI  NULL  auto_increment

answer varchar(250)      

polls_id int(11)     0

 

poll_results:

poll_results_id  int(10) unsigned    PRI  NULL  auto_increment

poll_answers_id int(11)     0

polls_id int(11)     0

user_ip varchar(32)  

 

 

Thanks in advance for any suggestive help!

 

 

Link to comment
https://forums.phpfreaks.com/topic/65827-php-sql-help/
Share on other sites

you can do it with one query but your row will have the following structure for example:

 

answer  | votes per this answer | total votes for the poll

answer  | votes per this answer | total votes for the poll

answer  | votes per this answer | total votes for the poll

 

i mean that it will repeat "total votes for the poll" for every answer from the same question

Link to comment
https://forums.phpfreaks.com/topic/65827-php-sql-help/#findComment-329754
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.