If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > List of records with count(*) in child table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-11-09, 09:44
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
List of records with count(*) in child table

hi
suppose i have 2 table (TBParent & TBChild) & every record in TBChild must have a relative record in TBParent. (a one to many relation between them).
in my application i want to show TBParent records list with count of TBChild records for every row. something like this:

TBParent Records List:

TBParentRow1: have 125 records in TBChild
TBParentRow2: have 1036 records in TBChild
TBParentRow3: have 0 records in TBChild

for this i have so many solutions,
first & most simple way is to SELECT TBParent Rows first, then execute a 'SELECT Count(*)' for every single row.
second one is create a view that count all TBChild records GROUP BY TBParent.id & then create another view from TBParent that have LEFT Join with first query.
now SELECT from second view has all columns plus count recs. but i think execute second query is very time-consuming for mysql.
but i don know what is most optimized solution.
Reply With Quote
  #2 (permalink)  
Old 05-11-09, 10:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
just one query
Code:
SELECT TBParent.id
     , COUNT(TBChild.parentID) AS children
  FROM TBParent 
LEFT OUTER
  JOIN TBChild
    ON TBChild.parentID = TBParent.id
GROUP
    BY TBParent.id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-12-09, 05:08
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
tanX, but this query is very time-consuming, my desktop went to coma!
Reply With Quote
  #4 (permalink)  
Old 05-12-09, 05:41
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
What are the indexes on the two tables?
You could use "show create table TBParent" etc to get this info.

Do you need to know how many children each parent has or do you just want to know which parent has no children?
Reply With Quote
  #5 (permalink)  
Old 05-15-09, 09:22
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
i need how many childeren each parent have.
Reply With Quote
  #6 (permalink)  
Old 05-15-09, 09:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by bono56
i need how many childeren each parent have.
then use the query in post #2

and make sure that you have the proper indexes to avoid another coma

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 05-20-09, 19:59
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
tHank you r937, & what about knowin which parent has no children?
Reply With Quote
  #8 (permalink)  
Old 05-20-09, 20:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by bono56
what about knowin which parent has no children?
well, those would be represented in the result set by a value of 0 in the children column

why do you ask? didn't you get any 0s when you tested it?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 05-27-09, 06:42
ashish_mat1979 ashish_mat1979 is offline
Registered User
 
Join Date: Aug 2005
Posts: 30
If you want to get only those parents who have no children you can try this:

Code:
SELECT TBParent.id
     , COUNT(TBChild.parentID) AS children
  FROM TBParent 
LEFT OUTER
  JOIN TBChild
    ON TBChild.parentID = TBParent.id
GROUP
    BY TBParent.id
HAVING
    children=0;
__________________
Ashish
Entertainment Overloaded
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On