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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:37:42+00:00 2026-05-18T12:37:42+00:00

Say we have this tables t1 ——————————– category_id | name | lft | rgt

  • 0

Say we have this tables

t1
--------------------------------
category_id | name | lft | rgt
--------------------------------
1             cat1   1     8
2             cat2   2     3
3             cat3   4     7
4             cat4   5     6

t2
-------------------------------
item_id | category_id
--------------------------------
 1          1
 2          2
 3          3
 4          4

Is there a way in MySQL to get the number of items a category has(including those items that belong to its children)? Something like this…

-------------------------------
category_id | item_count
--------------------------------
 1              4
 2              1
 3              2
 4              1

I have trouble connecting the query that gets the category_ids and the query that gets the count of the children categories.

SELECT category_id FROM t1 WHERE <some conditions here>

SELECT 
  COUNT(*) AS item_count,
  category_id
FROM
  t2
WHERE
  t2.category_id IN 
  (
 SELECT  
      node.category_id
 FROM 
  t1 AS node,
     t1 AS parent
    WHERE
  node.lft BETWEEN parent.lft AND parent.rgt
     AND parent.category_id = 5 <---- How do I pass the category ids of the first query
                                      here?
  )        
  • 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-18T12:37:43+00:00Added an answer on May 18, 2026 at 12:37 pm
    SELECT a.category_id, COUNT(a.item_id) as itemcount 
    FROM itemTable a 
    GROUP BY a.category_id;
    

    All you need to do is to find out how you can add the ones that don’t have any items to this.

    EDIT:
    Okay, so the problem is getting a value into a subselect…
    I’m not an expert at SQL, but I suggest creating a view.

    SELECT category_id FROM t1 WHERE <some conditions here>
    CREATE VIEW temp AS
     SELECT  
      node.category_id as nc_id, parent.category_id as pc_id
     FROM 
      t1 AS node,
      t1 AS parent
     WHERE
      node.lft BETWEEN parent.lft AND parent.rgt;
    
    VIEW temp
    nc_id | pc_id
    1   1
    2   1
    3   1
    4   1
    2   2
    3   2
    4   3
    
    
    t2
    itemid| catid
    1   1
    2   2
    3   3
    4   4
    
    CREATE VIEW temp2 AS
       SELECT 
         *
       FROM
         t2, temp
       WHERE t2.category_id = temp.nc_id OR t2.category_id = temp.pc_id
    
    cat_id| itemid |nc_id | pc_id
    1   1   1   1
    1   1   2   1
    1   1   3   1
    1   1   4   1
    2   2   2   1
    2   2   2   2
    2   2   3   2
    3   3   3   1
    3   3   3   2
    3   3   4   3
    4   4   4   1
    4   4   4   3
    
    CREATE VIEW temp3 AS
    SELECT cat_id, itemid, nc_id, pc_id
    FROM temp2
    GROUP BY item_id, nc_id;
    
    temp3:
    cat_id| itemid |nc_id | pc_id
    1   1   1   1
    1   1   2   1
    1   1   3   1
    1   1   4   1
    2   2   2   1
    2   2   3   2
    3   3   3   1
    3   3   4   3
    4   4   4   1
    
    SELECT count(itemid) AS itemcount, cat_id
    FROM temp3
    GROUP BY cat_id;
    
    itemcount      |cat_id
    4       1
    2       2
    2       3
    1       4
    
    DROP VIEW temp3;
    DROP VIEW temp2;
    DROP VIEW temp;
    

    And thus concludes my answer.
    I hope it works, and I also hope you can clean my mess up (in terms of optimizing what I wrote)

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

Sidebar

Related Questions

Let's say I have two tables that look like this: TH TH TH TH
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
I have multiple tables in a MySQL database. Lets say they look like this:
Lets say I have two tables like this: person: id, first_name, last_name, phone_id phone:
Say I have two tables (I am using Django, but this question is mostly
In this specific scenario, say I have a couple of tables {A,B} and their
For example, let's say that I have this 3 tables : Table Customers: CustomerID
Say I have two tables: orderinfo and customerinfo They look like this: orderinfo: OrderID
Say I have this table schema. ID AccNo Amount Say I have this data
I have this table: id feed_id ... Let's say that I have 500 rows

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.