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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:11:04+00:00 2026-05-28T04:11:04+00:00

I need to know if there is a possible way doing this with out

  • 0

I need to know if there is a possible way doing this with out subquery..

Here is my table structure:

id-name-father_id
1  joe    0
2  mark   0
3  muller 0
4  miki   2
5  timi   2
6  moses  2
7  david  1
8  momo   0
9  daniel 0
10 ermi   3

My table logic is

  • 0 means he is not a child of some one

  • 1+ mean that he is son of man in that row.

Note: if some one have a child, he still
will have 0 in father id (it’s mean there is not grand-fathers in my table)

My query is :

SELECT id, name, count(id=father_id) as sons
WHERE father_id = 0

What I want to get is a list of non-children (father_id=0) and sum
the childrens it has.
Is there a way to get the results without a subquery?

  • 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-28T04:11:04+00:00Added an answer on May 28, 2026 at 4:11 am

    This should do it (MySQL):

    SELECT `parents`.`id`, `parents`.`name`, COUNT(`children`.*) AS sons
    FROM `people` AS parents
    LEFT JOIN `people` AS children ON `parents`.`id` = `children`.`father_id`
    WHERE `parents`.`father_id` = 0
    GROUP BY `parents`.`id`
    

    According to Gary we need to add name to GROUP BY in other SQL databases:

    SELECT `parents`.`id`, `parents`.`name`, COUNT(`children`.*) AS sons
    FROM `people` AS parents
    LEFT JOIN `people` AS children ON `parents`.`id` = `children`.`father_id`
    WHERE `parents`.`father_id` = 0
    GROUP BY `parents`.`id`, `parents`.`name`
    

    We are joing the table with itself here. So we join all parents with their children.

    This will lead to a result like that:

    parents.id  parents.name children.id  children.name
    1           joe          7            david
    2           mark         4            miki
    2           mark         5            timi
    2           mark         6            moses
    3           muller       10           ermi
    8           momo         -            - # left join allows this line
    9           daniel       -            -
    

    But now we have each parent several times. So we are GROUP’ing the whole thing over the parent’s id, which will result in the following:

    parents.id  parents.name COUNT(children.*)
    1           joe          1
    2           mark         3
    3           muller       1
    8           momo         0
    9           daniel       0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know if there's any way (or another distinct approach) to an
I need to know if there's a way in PHP to echo a string
I need to know if there is a way to create a custom shortcut
I know there has to be a way: I need help getting any hot
I need something like a temporary in-memory key-value store. I know there are solutions
I'm need to use JUnit 3, I know in JUnit 4 there is a
As you may know, in many occasions, there is a need to flag some
Need to know this so that i could send DTMF and that is going
Possible Duplicate: Is there an “exists” function for jQuery Is there a better way
Need to know which event triggered after text selection done in Apple iPad. It

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.