Solved

is there a way to do this in  single query?

Posted on 2012-03-29
2
424 Views
Last Modified: 2012-03-29
hello,
i have a pretty simple table.

the parent tc_group record has a parentID of 0. A tc_group can have children and that would be indicated by the parentID

CREATE TABLE `tc_group` (
      `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
      `userID` INT(10) UNSIGNED NULL DEFAULT NULL,
      `group_name` VARCHAR(50) NULL DEFAULT NULL,
      `parentID` INT(10) UNSIGNED NULL DEFAULT '0',
      `idx` SMALLINT NULL DEFAULT '0',
      PRIMARY KEY (`id`)


What i would like to do but can not seem to think of a way to do it is with One query get all the rows that have a parentID = 0  and a count of how many rows each of those records have were its own ID is the parentID. So get all the parents and a count of their children with a single query.

I am thinking maybe i could do this with a subselect but jsut can't seem to get my head around it
thanks for any help
0
Comment
Question by:paries
2 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 37784275
try this:

select g.*, (select count(1) from tc_group g2 where g2.parentID=g.id) as ChildCount
from tc_group g
where parentid=0
0
 

Author Closing Comment

by:paries
ID: 37784283
thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now