Solved

is there a way to do this in  single query?

Posted on 2012-03-29
2
417 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Creating and Managing Databases with phpMyAdmin in cPanel.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

18 Experts available now in Live!

Get 1:1 Help Now