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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:27:34+00:00 2026-06-15T19:27:34+00:00

I have one stuff in one table (A) and category of it in two

  • 0

I have one stuff in one table (A) and category of it in two others (C1,C2)
category_id coming from different table (MC).

I would like to count how many stuffs presented in particular category.

Example of live data:

table A

main_key (unique)    stuff_id (non unique)
   1                    7
   2                    6 
   3                    3

table C1

category_id main_key (it is FK for A table)
   1             1
   1             2
   3             1

table C2

category_id main_key (it is FK for A table)
   2             3
   2             1

table MC

category_id category_name
   1             blablbl
   2               asas
   3             asasa
...

relation between:
C1 and A as many to one
C2 and A as many to one
C1 or C2 and MC as many to one

In current example I would like to see final result as

stuff_qnt        category_id        category_name
2                   1                  blablbl
2                   2                   asas
1                   3                   asasa

how I can achieve it with one single query ?

my query is:

SELECT count(A.stuff_id) as stuff_qnt, MC.category_id, MC.category_name 
FROM A
LEFT JOIN C1 using(main_key)
LEFT JOIN C2 using(main_key)
LEFT JOIN MC ON (C1.category_id = MC.category_id AND C2.category_id = MC.category_id)
GROUP BY C1.category_id, C2.category_id

But it shows me wrong result, what I’m doing wrong ?

  • 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-06-15T19:27:35+00:00Added an answer on June 15, 2026 at 7:27 pm
    SELECT COUNT(*) AS Stuff_Qnt, C.Category_ID, MC.Category_Name
      FROM MC
      JOIN (SELECT C1.Category_ID
              FROM A
              JOIN C1 ON A.Main_Key = C1.Main_Key
            UNION ALL
            SELECT C2.Category_ID
              FROM A
              JOIN C2 ON A.Main_Key = C2.Main_Key
           ) AS C
        ON C.Category_ID = MC.Category_ID
     GROUP BY C.Category_ID, MC.Category_Name
     ORDER BY C.Category_ID, Stuff_Qnt;
    

    You need the categories from joining A with C1 and the categories from joining A with C2, and you definitely don’t want a Cartesian product such as you’d get with LEFT OUTER JOIN, so you take the UNION of the lists of category IDs, and then aggregate and join with MC.

    CREATE TABLE A
    (
        main_key INTEGER NOT NULL PRIMARY KEY,
        stuff_id INTEGER NOT NULL
    );
    INSERT INTO A VALUES(1, 7);
    INSERT INTO A VALUES(2, 6);
    INSERT INTO A VALUES(3, 3);
    
    CREATE TABLE MC
    (
        category_id INTEGER NOT NULL PRIMARY KEY,
        category_name VARCHAR(10) NOT NULL
    );
    INSERT INTO mc VALUES(1, "blablbl");
    INSERT INTO mc VALUES(2, "asas");
    INSERT INTO mc VALUES(3, "asasa");
    
    CREATE TABLE C1
    (
        category_id INTEGER NOT NULL REFERENCES mc,
        main_key INTEGER NOT NULL REFERENCES a
    );
    INSERT INTO c1 VALUES(1, 1);
    INSERT INTO c1 VALUES(1, 2);
    INSERT INTO c1 VALUES(3, 1);
    
    CREATE TABLE C2
    (
        category_id INTEGER NOT NULL REFERENCES mc,
        main_key INTEGER NOT NULL REFERENCES a
    );
    INSERT INTO c2 VALUES(2, 3);
    INSERT INTO c2 VALUES(2, 1);
    
    SELECT COUNT(*) AS Stuff_Qnt, C.Category_ID, MC.Category_Name
      FROM MC
      JOIN (SELECT C1.Category_ID
              FROM A
              JOIN C1 ON A.Main_Key = C1.Main_Key
            UNION ALL
            SELECT C2.Category_ID
              FROM A
              JOIN C2 ON A.Main_Key = C2.Main_Key
           ) AS C
        ON C.Category_ID = MC.Category_ID
     GROUP BY C.Category_ID, MC.Category_Name
     ORDER BY C.Category_ID, Stuff_Qnt;
    

    Output:

    2   1   blablbl
    2   2   asas
    1   3   asasa
    

    Here is a further test with two extra rows in A, and corresponding rows in C1 and C2. There are two queries tested, mine and the query by dkkumargoyal.

    CREATE TABLE A(main_key INTEGER NOT NULL PRIMARY KEY, stuff_id INTEGER NOT NULL);
    INSERT INTO A VALUES(1, 7);
    INSERT INTO A VALUES(2, 6);
    INSERT INTO A VALUES(3, 3);
    INSERT INTO A VALUES(4, 3);
    INSERT INTO A VALUES(5, 3);
    
    CREATE TABLE MC(category_id INTEGER NOT NULL PRIMARY KEY, category_name VARCHAR(10) NOT NULL);
    INSERT INTO mc VALUES(1, "blablbl");
    INSERT INTO mc VALUES(2, "asas");
    INSERT INTO mc VALUES(3, "asasa");
    
    CREATE TABLE C1(category_id INTEGER NOT NULL REFERENCES mc, main_key INTEGER NOT NULL REFERENCES a);
    INSERT INTO c1 VALUES(1, 1);
    INSERT INTO c1 VALUES(1, 2);
    INSERT INTO c1 VALUES(3, 1);
    INSERT INTO c1 VALUES(3, 4);
    INSERT INTO c1 VALUES(1, 4);
    INSERT INTO c1 VALUES(1, 5);
    
    CREATE TABLE C2(category_id INTEGER NOT NULL REFERENCES mc, main_key INTEGER NOT NULL REFERENCES a);
    
    INSERT INTO c2 VALUES(2, 3);
    INSERT INTO c2 VALUES(2, 1);
    INSERT INTO c2 VALUES(2, 5);
    
    SELECT COUNT(*) AS Stuff_Qnt, C.Category_ID, MC.Category_Name
      FROM MC
      JOIN (SELECT C1.Category_ID
              FROM A
              JOIN C1 ON A.Main_Key = C1.Main_Key
            UNION ALL
            SELECT C2.Category_ID
              FROM A
              JOIN C2 ON A.Main_Key = C2.Main_Key
           ) AS C
        ON C.Category_ID = MC.Category_ID
     GROUP BY C.Category_ID, MC.Category_Name
     ORDER BY C.Category_ID, Stuff_Qnt;
    
    -- Query by dkkumargoyal
    SELECT COUNT(DISTINCT A.stuff_id) AS stuff_qnt, MC.category_id, MC.category_name 
    FROM A
    LEFT JOIN C1 on a.main_key = c1.main_key  -- USING(main_key)
    LEFT JOIN C2 on a.main_key = c2.main_key  -- USING(main_key)
    LEFT JOIN MC ON (C1.category_id = MC.category_id OR C2.category_id = MC.category_id)
    GROUP BY MC.category_id, mc.category_name
    ORDER BY MC.Category_id, stuff_qnt;       -- stuff_qnt added for standard compatibility
    

    The changes made were necessary for the code to work on the test DBMS (Informix 11.70.FC6).

    Results 1:

    4   1   blablbl
    3   2   asas
    2   3   asasa
    

    Results 2:

    3   1   blablbl
    2   2   asas
    2   3   asasa
    

    I think my result is correct and the other not, mainly because the alternative depends on A.Stuff_ID being unique when the question stipulates that it is not unique (and the additional rows of data make it non-unique).

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

Sidebar

Related Questions

i have a string like this one: $string = some text http://dvz.local/index/index/regionId/28 http://stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php
i have a mysql db, and i have one table which has many columns
G'day! I have one million different words which I'd like to query for in
I want to create Many-One Mapping between two tabels, Expense(ID, NAME, CATEGORY) and Category(ID,
I currently have a table like this: Stuff ---------- StuffId identity int not null
I have been doing PHP stuff for almost one year and I have never
have one time consuming step that flattens a bunch of files. basically i'd like
I have one user (only one, all the others are fine) trying to update
I have one drop down list in my page, which contains two options. What
I have one model User and will be creating another one Category . Both

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.