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

  • Home
  • SEARCH
  • 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 6219157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:43:53+00:00 2026-05-24T07:43:53+00:00

I have a table that looks like this: Id | Name | Parent —+————————-+——-

  • 0

I have a table that looks like this:

Id | Name                    | Parent
---+-------------------------+-------
1  | Parent One              | 0
2  | Child of Parent One     | 1
3  | Parent Two              | 0
4  | Parent Three            | 0
5  | Parent Four             | 0
6  | Child of 1st Parent     | 1
7  | Child of 2nd Parent     | 3
8  | Child of 3nd Parent     | 4

The table does not represent a hierarchy: Every item is either a child or a parent, but not both.

I’d like to run a query on it that returns this:

Id | Name                    | ChildCount
---+-------------------------+-----------
1  | Parent One              | 2
3  | Parent Two              | 1
4  | Parent Three            | 1
5  | Parent Four             | 0

I guessed that this might work, but it didn’t:

SELECT parents.id, parents.name, COUNT(parents.id = children.parent) AS childCount
FROM (SELECT * FROM items WHERE parent = 0) parents,
     (SELECT * FROM items WHERE parent > 0) children

How should I be doing this?

  • 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-24T07:43:53+00:00Added an answer on May 24, 2026 at 7:43 am
    SELECT a.id, a.Name, COUNT(b.id) as ChildCount
    FROM table1 a
    LEFT JOIN table1 b ON (b.Parent = a.id)
    GROUP BY a.id [,a.Name] // ,a.Name is not mandatory for mysql, `GROUP BY a.id` is enough
    

    You may also want to add WHERE a.Parent = 0 to show only parent rows.

    Updated (COUNT(*) changed to COUNT(b.id) )

    For Eldest Child:

    SELECT x.id, x.Name, x.ChildCount, c.Name AS eldest_child_name
    FROM
    (
    SELECT a.id, a.Name, COUNT(b.id) as ChildCount, MAX(b.id) as max_child_id
    FROM table1 a
    LEFT JOIN table1 b ON (b.Parent = a.id)
    GROUP BY a.id 
    )X
    LEFT JOIN table1 c ON (c.id = X.max_child_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a categories table that looks like this: id | name | parent
I have one table that looks something like this: id parent_id name 1 0
I have a table that looks like this: id datatype name value 0 nvarchar(255)
I have a table that looks like this user_id | name | created_on |
I have a table that looks like this: category category_id name category_seo_friendly_url left_id right_id
So I have a table that looks like this: Name ID TaskID HoursAssigned ----------------------------------------------------------------
My parent-child table looks like this: ID, ParentID, Name, Population 1, Null, Asia, 40
I have a table that looks a bit like this actors(forename, surname, stage_name); I
I have a table that looks like this: nid vid tid 1 2 3
I have a table that looks like this: <table> <thead> <!-- random table headings

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.