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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:04:47+00:00 2026-05-28T08:04:47+00:00

I have 3 tables: problems, categories, and a joining table for problem_categories I am

  • 0

I have 3 tables: problems, categories, and a joining table for problem_categories

I am trying to get and count all the categories associated with a problem, and count how many problems are associated with each problem.

Here is my query which doesn’t return correct results:

SELECT 
  category_name , 
  categories.category_id , 
  problems.problem_id, 
  COUNT(problems.problem_id) as num_problems 
FROM 
  problem_categories 
left JOIN 
  categories 
on 
  problem_categories.category_id = categories.category_id 
left join 
  problems 
on
  problem_categories.problem_id = problems.problem_id 
WHERE 
  problem_categories.problem_id = 266
GROUP BY 
  problems.problem_id , category_name

but as soon as I take out the where clause, it returns all the categories and correct counts of problems. But the trick is that I need to query this for a particular problem_id

Any way to adjust this query to get it right? What am I doing incorrectly here?

Thanks!!

  • 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-28T08:04:48+00:00Added an answer on May 28, 2026 at 8:04 am

    Your question is somewhat unclear. How can you count problems if you are selecting a specific one? If you want to count categories associated with a particular problem you do this (I am assuming you have foreign keys and no duplicates in the problem_categories table):

    SELECT problem_id, COUNT(category_id) as num_categories
    FROM problem_categories
    WHERE problem_id = 266
    GROUP BY problem_id
    

    then join to problem table if you need to print out more problem information (for efficiency, always get the detail last). If you are wanting to count problem -> category -> problem then do:

    select p1.problem_id, (COUNT(DISTINCT(p2.problem_id)) - 1) as num_problems
    FROM
    (select problem_id, category_id FROM problem_categories where problem_id = 266) as p1
    inner join 
    (select problem_id, category_id FROM problem_categories) as p2
    ON p1.category_id = p2.category_id
    GROUP by p1.problem_id
    

    Again if you want to show the problem details, join to the problem table to get them. If you want the num_problems and num_categories together join the first query to the second one before joining to the problem table to get the problem detail. i.e.:

    SELECT p1.problem_id, p1.num_categories, p2.num_problems 
    FROM
    (SELECT problem_id, COUNT(category_id) as num_categories
    FROM problem_categories
    WHERE problem_id = 266
    GROUP BY problem_id) as p1
    INNER JOIN
    (SELECT p1.problem_id, (COUNT(DISTINCT(p2.problem_id)) - 1) as num_problems
    FROM
    (SELECT problem_id, category_id FROM problem_categories where problem_id = 266) as p1
    INNER JOIN 
    (SELECT problem_id, category_id FROM problem_categories) as p2
    ON p1.category_id = p2.category_id
    GROUP by p1.problem_id) as p2
    ON p1.problem_id = p2.problem_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some problems trying to perform a query. I have two tables, one
While trying to use LINQ to SQL I encountered several problems. I have table
I have a MySQL Left Join problem. I have three tables which I'm trying
I have tables with listings, categories and one that maps them to each other.
I have 3 Tables: Regions Table which has 1500 Towns in it. Categories Table
Let's say I have two tables in my database. TABLE:Categories ID|CategoryName 01|CategoryA 02|CategoryB 03|CategoryC
I have two tables, categories and movies . In movies table I have a
I am trying to have categories in my budget2000 table be the foreign key
I have three tables being used for this problem: songs, blacklist, and whitelist. The
The problem: let's say I have two tables Client , and Product , in

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.