Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:48:46+00:00 2026-05-27T03:48:46+00:00

I’m struggling with a task that could be very simple but I’m having a

  • 0

I’m struggling with a task that could be very simple but I’m having a hard time figuring it out. I’ve two database table:

options_table

option_id | option_name | question_id |
    1     |   opt one   |      4      |
    2     |   opt two   |      4      |
    3     |   opt three |      4      |
    4     |   opt four  |      4      |

and votes_table

question_id | survey_id | option_id |
    4       |    1      |     1     |
    4       |    1      |     2     |
    4       |    1      |     2     |
    4       |    1      |     1     |
    4       |    1      |     3     |
    4       |    1      |     2     |

From this example, we have 2 votes on option 1, 3 votes on option 2 and 1 vote on option 3, but none on option 4.
I’d like to retrieve all the options name with the relative count, but I can’t get the option without votes. I’m sure I’m overlooking something, it’s workday’s end and I must be tired.
I tried something like:

   SELECT o.option_name, count(*) as cnt 
   FROM votes_table AS v 
   JOIN options_table AS o 
   ON o.option_id = v.option_id 
   WHERE v.survey_id = 1 
   GROUP BY o.option_name

but I just get

option_name | option_id | cnt
 opt one    |   1       |  2
 opt two    |   2       |  3
 opt three  |   3       |  1

How do I get also “opt four”? So I can properly display all the survey results including the option noone voted. I also tried using OUTER JOINs, but to no avail.

I’m ashamed for this might sound a stupid question, but I still have a lot to learn on databases and relationships

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:48:46+00:00Added an answer on May 27, 2026 at 3:48 am

    Use LEFT instead of INNER join – put in the Left side the table with all the options, and in the Right side the table with missing options.

    Move any condition that uses a column from the Right table, like the v.survey_id = 1 one, from the WHERE to the ON clause.

    Use COUNT(RightTable.column) instead of COUNT(*), so you get 0 for options not voted:

    SELECT o.question_id
         , o.option_name
         , COUNT(v.option_id) as cnt 
    FROM 
        options_table AS o  
      LEFT JOIN 
        votes_table AS v
          ON  v.option_id = o.option_id 
          AND v.question_id = o.question_id
          AND v.survey_id = 1 
    WHERE o.question_id = 4            --- if you want results for one question only 
    GROUP BY o.question_id
           , o.option_id
    

    I suppose that (option_id, question_id) is the Primary (or a Unique) Key of the options table. That’s why you either need to GROUP BY both of them or WHERE o.question_id and GROUP BY o.option_id.

    Otherwise, if you only GROUP BY o.option_id, the query for every option, will count votes for all questions that were voted with this option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.