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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:49:38+00:00 2026-05-25T23:49:38+00:00

I have a problem counting a table joined several times. The question table :

  • 0

I have a problem counting a table joined several times.

The question table :

+----+----------+
| id | question |
+----+----------+
|  1 | Foo?     |
+----+----------+

The answer one :

+----+-------------+--------+
| id | question_id | choice |
+----+-------------+--------+
|  1 |           1 |      1 |
|  2 |           1 |      1 |
|  3 |           1 |      1 |
|  4 |           1 |      2 |
|  5 |           1 |      3 |
|  6 |           1 |      3 |
+----+-------------+--------+

The expected result :

+----------+-------+-------+-------+
| question | num_1 | num_2 | num_3 |
+----------+-------+-------+-------+
| Foo?     |     3 |     1 |     2 |
+----------+-------+-------+-------+

The (failing) query and its result :

SELECT
    q.question AS question,
    COUNT(a1.id) AS num_1,
    COUNT(a2.id) AS num_2,
    COUNT(a3.id) AS num_3
FROM
    question q
    LEFT JOIN answer a1 ON a1.question_id = q.id AND a1.choice = 1
    LEFT JOIN answer a2 ON a2.question_id = q.id AND a2.choice = 2
    LEFT JOIN answer a3 ON a3.question_id = q.id AND a3.choice = 3
GROUP BY
        q.id

+----------+-------+-------+-------+
| question | num_1 | num_2 | num_3 |
+----------+-------+-------+-------+
| Foo?     |     6 |     6 |     6 |
+----------+-------+-------+-------+

I don’t understand why I get this result.
Can you help me?

  • 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-25T23:49:39+00:00Added an answer on May 25, 2026 at 11:49 pm

    Because choice = 1 gives 3 rows, choice = 2 gives 1 row, choice = 3 gives 2 rows and 1 * 2 * 3 = 6. if you remove the group by and aggregates and look at the results it should be clear. You can use

    SELECT
        q.question AS question,
        COUNT(CASE WHEN a.choice = 1 THEN 1 END) AS num_1,
        COUNT(CASE WHEN a.choice = 2 THEN 1 END) AS num_2,
        COUNT(CASE WHEN a.choice = 3 THEN 1 END) AS num_3
    FROM
        question q
        LEFT JOIN answer a ON a.question_id = q.id AND a.choice IN (1,2,3)
    GROUP BY
            q.id,
            q.question
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with Counting sort. When i will sort table sometimes it
I am having a problem counting my rows in Konkurrancers table. I have tried
I have following problem: I want to count data in one table, count data
So... I have a problem with counting all ccurrences of preg_match and summing/addition first
While my initial problem counting the running number of rows in a table has
I have more of a 'problem solving' question than a syntax related problem. Briefly,
I have problem with joining tables, here are example tables: Table A: ( 30
I have a table: Hourly_Sales that is like this (counting how many buy something
Today's problem is as follows: we have a table in an Oracle database. The
Apologies in advance if this question is badly worded! I have a MySQL table

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.