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

The Archive Base Latest Questions

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

I have this tag table, tag_id tag_name parent_id cat_id 3 Tagname-1 NULL NULL 5

  • 0

I have this tag table,

tag_id  tag_name    parent_id   cat_id
3       Tagname-1   NULL        NULL    
5       tagname-2   NULL        NULL    
6       tagname-3   NULL        NULL
9       tagname-4   NULL        NULL
11      tagname-5   3           NULL
13      tagname-6   3           NULL
15      tagname-8   5           NULL
17      tagname-9   5           NULL
18      tagname-10  NULL        NULL
20      tagname-11  6           NULL
22      tagname-12  9           NULL
24      tagname-13  NULL        NULL
26      tagname-14  NULL        NULL
28      tagname-15  NULL        NULL

I want to return a result like this,

ParentID    ParentName  TotalChildren
3           Tagname-1   2
5           tagname-2   2
6           tagname-3   1
9           tagname-4   1
18          tagname-10  0
24          tagname-13  0
26          tagname-14  0
28          tagname-15  0

So here the query I came out with so far,

SELECT 
a.tag_id as ParentID,
a.tag_name as ParentName,
b.TotalChildren

FROM root_tags a INNER JOIN
(
    SELECT parent_id, COUNT(1) as TotalChildren
    FROM root_tags
    WHERE parent_id <> tag_id
    GROUP BY parent_id
) b 

ON a.tag_id = b.parent_id
ORDER BY ParentID

But, unfortunately, it only return the result like this,

ParentID    ParentName  TotalChildren
3           Tagname-1   2
5           tagname-2   2
6           tagname-3   1
9           tagname-4   1

Which means it is missing the parents without any children.

How can I make the tags without children to become parents too? or in other words, how to make the tags without parents to become parents themselves?

EDIT:

SELECT 
a.tag_id as ParentID,
a.tag_name as ParentName,
b.TotalChildren

FROM root_tags a LEFT OUTER JOIN
(
    SELECT parent_id, COUNT(1) as TotalChildren
    FROM root_tags
    WHERE parent_id <> tag_id
    GROUP BY parent_id
) b 

ON a.tag_id = b.parent_id
ORDER BY ParentID

the answer above returns,

ParentID    ParentName  TotalChildren
3           Tagname-1   2
5           tagname-2   2
6           tagname-3   1
9           tagname-4   1
11          tagname-5   NULL
13          tagname-6   NULL
15          tagname-8   NULL
17          tagname-9   NULL
18          tagname-10  NULL
20          tagname-11  NULL
22          tagname-12  NULL
24          tagname-13  NULL
26          tagname-14  NULL
28          tagname-15  NULL

which is incorrect as it returns all the children.

  • 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:01:09+00:00Added an answer on May 24, 2026 at 7:01 am

    Prefer a Join against a Subquery:

    SELECT parents.tag_id AS ParentID,
           parents.tag_name AS ParentName,
           COUNT(childs.tag_id) AS TotalChildren
    FROM root_tags AS parents
        LEFT OUTER JOIN root_tags AS childs
            ON parents.tag_id = childs.parent_id
    WHERE parents.parent_id IS NULL
    GROUP BY parents.tag_id, parents.tag_name
    ORDER BY parents.tag_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: id | id_parent | tag_name 0 | |
I have tag like this, how the best way to get every key and
I have a transaction table, and a tag table. I want to get a
I have a table of categories (tags) in list_tags : tag_id tag_name 1 social
I have this custom JSP tag that implements tree table(every task can have sub-tasks):
I have this tag into my html file: {{block:my_test_block}} {{news:my_test_block2}} I need to parse
I have this tag: <a id=link1 runat=server href=http://www.selab.isti.cnr.it/ws-mate/example.pdf title=PDF>Open iFrame</a> This link opens a
i have this tag as input tag: <a href=controller.jsp?sid=127490C88DB5&R=35144 class=11-link-dkred-bold><b>Mr. John Q. Anderson&nbsp;&nbsp;&nbsp;MBA 1977
I have this form tag below where the form submits to itself which I
I have this code which can display drop down in div tag of html.

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.