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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:58:24+00:00 2026-05-16T19:58:24+00:00

I tried .. SELECT c.* , ( SELECT COUNT( * ) FROM item t

  • 0

I tried ..

SELECT  c.* , (
    SELECT COUNT( * )
    FROM item t
    WHERE t.cat_id = c.cat_id
    )ct_items, (

    SELECT COUNT( * )
    FROM item t
    INNER JOIN cat c3 ON t.cat_id = c3.cat_id
    AND c3.cat_id = c.parent_id
    ) ct_sub
FROM cat c
WHERE parent_id = '0'
ORDER BY name

but got Unknown column ‘c.parent_id’ in ‘on clause’. Any ideas why I am getting this or another way to achieve this using mysql query? I can workout the numbers using multiple queries and using php etc though.

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-16T19:58:25+00:00Added an answer on May 16, 2026 at 7:58 pm

    You don’t necessarily have to do everything in one query; sometimes trying to glue queries together ends up with worse performance (especially when correlated subqueries are involved). Two queries is OK; it’s when you end up calling a new query for each row you’ve got problems.

    So you could get the category items:

    SELECT c0.*, COUNT(i0.id) AS cat_nitems
    FROM cat AS c0
    LEFT JOIN item AS i0 ON i0.cat_id=c0.cat_id
    WHERE c0.parent_id= '0'
    GROUP BY c0.cat_id
    ORDER BY c0.name
    

    and then separately get the subcategory items using a parent-child self-join:

    SELECT c0.*, COUNT(i1.id) AS subcats_nitems
    FROM cat AS c0
    LEFT JOIN cat AS c1 ON c1.parent_id=c0.cat_id
    LEFT JOIN item AS i1 ON item.cat_id=c1.cat_id
    WHERE c0.parent_id= '0'
    GROUP BY c0.cat_id
    ORDER BY c0.name
    

    And yes, you can join them both into a single query:

    SELECT c0.*, COUNT(DISTINCT i0.id) AS cat_nitems, COUNT(DISTINCT i1.id) AS subcats_nitems
    FROM cat AS c0
    LEFT JOIN cat AS c1 ON c1.parent_id=c0.cat_id
    LEFT JOIN item AS i0 ON item.cat_id=c0.cat_id
    LEFT JOIN item AS i1 ON item.cat_id=c1.cat_id
    WHERE c0.parent_id= '0'
    GROUP BY c0.cat_id
    ORDER BY c0.name
    

    I would suspect the larger join and DISTINCT processing might make it less efficient. But then I guess on a small database you ain’t gonna notice.

    Either way, this will only work for two-deep nesting. If you need sub-sub-categories or arbitrary-depth trees in general, you should consider a schema that’s better at modelling trees, such as nested sets.

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

Sidebar

Related Questions

I tried the following select: SELECT (id,name) FROM v_groups vg INNER JOIN people2v_groups p2vg
I tried with this query but no luck : SELECT tr.transferfrom, tr.transferto, br.id as
How do I select the globally-last text node using xpath? I tried this, but
What I tried is by adding selected , but this is not working: <select>
I have the following table: OrderHasItem OrderTable_idOrder Item_idItem quantity SELECT Item_idItem, COUNT(Item_idItem)+quantity FROM OrderHasItem
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Having tried this query a million different ways, I can't seem to SELECT 'count_lonely_vehicles'
What's wrong with the code of radio button? When I've tried SELECT/OPTION it's working.
I have tried following queries to get my facebook fan page insights data SELECT
I have tried aligning the text in the <select> element to the right or

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.