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 697687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:11:37+00:00 2026-05-14T03:11:37+00:00

I have 2 mysql tables : Question with the following columns : id, question,

  • 0

I have 2 mysql tables :

Question with the following columns : id, question, nranswers

Nranswers must be a number from 1 to 5

And the other table is

Answers with the following columns: questionid, userid, answer .

Now the problem is that I want to get the replies for each answer for one question(id 22 let’s say) .

P.S. If the nranswers is 3, the result should look like this:

(the right number means how many times the reply number was chosen)

1 – 2

2 – 8

3 – 7

If the nranswers is 5, the result should look like this:

1 – 3

2 – 8

3 – 14

4 – 19

5 – 8

Please help me out with the query, atm he’s not counting the answers that weren’t chosen, only the ones that were chosen at least one time.

  • 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-14T03:11:37+00:00Added an answer on May 14, 2026 at 3:11 am

    I took the liberty of adding the question_id column that will be used to join each answer with a question.

    Question with the following columns : id, question, nranswers
    Answers with the following columns: question_id, userid, answer
    

    Here’s your query:

    SELECT answer, COUNT(*) AS answer_count
    FROM Answers
    GROUP BY answer
    WHERE question_id = 22
    ORDER by answer
    

    However, if nranswers is 3, but nobody picked 3 as their answer, it won’t show. This query only shows the answers that were chosen.

    Edit:

    To get a count of all available answers, not just the selected ones, the simplest way (query wise) would be to get rid of the Question.nranswers column and add the table QuestionAnswers:

    QuestionAnswers with the following columns: question_id, answer
    

    The data in QuestionAnswers would like this:

    quesiton_id answer
    -------------------
    22          1
    22          2
    22          3
    

    So, you’d have all the possible answers listed for each question.

    The query would then be this:

    SELECT qa.answer, COUNT(a.question_id) AS answer_count
    FROM QuestionAnswers qa
    LEFT OUTER JOIN Answers a
      ON qa.question_id = a.question_id AND a.answer = qa.answer
    GROUP BY qa.answer
    WHERE qa.question_id = 22
    ORDER by qa.answer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 mysql tables 1. questions: with the following columns: id, title, answer1,
Following my previous question . I have a table named activity with 2 columns:
I have the following tables category and category_description in php mysql. my question is
I have a MySQL(innodb) table 'items' with the following characteristics Large number of rows,
I have a question about tables in MySQL. I'm currently making a website where
I have several tables that get JOINed together to form a table with columns
This is a follow up from my question: MySQL: Querying a table and possibly
I have the following script to create a table in MySQL version 5.1 which
I have a MySQL database with the following table structure: TransactionType : Transaction_Amount, Transaction_ID,
I have 2 MySQL tables with the following schemas for a web site that's

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.