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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:16:07+00:00 2026-06-18T12:16:07+00:00

Say I have a table: user_id parent_id lev1 lev2 lev3 lev4 1 0 0

  • 0

Say I have a table:

user_id    parent_id    lev1    lev2   lev3    lev4
1          0            0       0      0       0
2          1            1       0      0       0
3          1            1       0      0       0
4          2            2       1      0       0
5          4            4       2      1       0
6          4            4       2      1       0
7          5            5       4      2       1

Basically, this is to keep track of parent-children hierarchy and I want to get how many children does a parent have. Below is the output I want:

parent_id     children
1             5
2             4
3             0
4             3
5             1
6             0
7             0

I’d like to COUNT the combined lev1, lev2, lev3 and lev4 fields to count how many IDs all in all in those fields.

I read about UNION ALL but I can’t seem to figure out how it operates. I’m thinking of a UNION ALL with a self JOIN?

  • 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-18T12:16:08+00:00Added an answer on June 18, 2026 at 12:16 pm

    You need a LEFT JOIN against a subquery for each levN column, which returns the distinct level, and count for that column. They all then get added up and joined to user_id.

    SELECT
      DISTINCT
      user_id,
      /* COALESCE() is needed so NULLs don't ruin the calculation */
      COALESCE(l1count, 0) +
      COALESCE(l2count, 0) +
      COALESCE(l3count, 0) +
      COALESCE(l4count, 0) AS children
    FROM
      yourtable
      /* a left join individually against each of the `levN` columns to get the count per value of each */
      LEFT JOIN (SELECT lev1, COUNT(*) AS l1count FROM yourtable GROUP BY lev1) l1 ON yourtable.user_id = l1.lev1
      LEFT JOIN (SELECT lev2, COUNT(*) AS l2count FROM yourtable GROUP BY lev2) l2 ON yourtable.user_id = l2.lev2
      LEFT JOIN (SELECT lev3, COUNT(*) AS l3count FROM yourtable GROUP BY lev3) l3 ON yourtable.user_id = l3.lev3
      LEFT JOIN (SELECT lev4, COUNT(*) AS l4count FROM yourtable GROUP BY lev4) l4 ON yourtable.user_id = l4.lev4
    

    http://sqlfiddle.com/#!2/214a8/16

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

Sidebar

Related Questions

Say I have a table like this: create table users ( user_id int not
Let's say I have a table that has user_id, date, score, and every user
Say I have a 'user_log' table with the following field: id user_id status_text timestamp
Say I have a table that goes clockwise like this: 2 1 3 0
Say I have this table schema. ID AccNo Amount Say I have this data
Lets say I have the following table User_ID Manager_ID --------------------- Linda Jacob Mark Linda
Say i have a table 'users', with 'user_id', 'name' and 'multiplier'. If I do
Lets say we have a table Requests with structure: user_id product_id count With entries
Say I have the following table: create table ratings ( user_id int unsigned not
Ok say i have 1 table for Users it stores USER_NAME, USER_ID, AND USER_COLOR

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.