Thread: Count the children

Count the children

From
Michael Rowan
Date:
I need what sounds like a very simple query - but I'm darned if I can
figure it out.

I have two tables, parent and child related by child.parent_id =
parent.id
The query should return, for each parent row, parent.name and number
of child rows

Can someone enlighten me, please?

Michael Rowan
mike.rowan@internode.on.net

11 Kingscote Street
ALBERTON
South Australia 5014

tel 618 8240 3993




Re: Count the children

From
Sean Davis
Date:


On Tue, Aug 18, 2009 at 10:34 AM, Michael Rowan <mike.rowan@internode.on.net> wrote:
I need what sounds like a very simple query - but I'm darned if I can figure it out.

I have two tables, parent and child related by child.parent_id = parent.id
The query should return, for each parent row, parent.name and number of child rows

Hi, Michael.

select parent.name,count(*) from parent join child on child.parent_id=parent.id;

Or am I missing something a subtlety?

Sean


Re: Count the children

From
Mark Styles
Date:
On Wed, Aug 19, 2009 at 12:04:13AM +0930, Michael Rowan wrote:
> I need what sounds like a very simple query - but I'm darned if I can
> figure it out.
>
> I have two tables, parent and child related by child.parent_id =
> parent.id
> The query should return, for each parent row, parent.name and number of
> child rows
>
> Can someone enlighten me, please?

Assuming parent.name is unique:

SELECT parent.name, count(*)
FROM   parent, child
WHERE  parent.key = child.parent_key
GROUP BY parent.name;

--
Mark
http://www.lambic.co.uk


Attachment

Re: Count the children

From
Sean Davis
Date:


On Tue, Aug 18, 2009 at 10:50 AM, Sean Davis <sdavis2@mail.nih.gov> wrote:


On Tue, Aug 18, 2009 at 10:34 AM, Michael Rowan <mike.rowan@internode.on.net> wrote:
I need what sounds like a very simple query - but I'm darned if I can figure it out.

I have two tables, parent and child related by child.parent_id = parent.id
The query should return, for each parent row, parent.name and number of child rows

Hi, Michael.

select parent.name,count(*) from parent join child on child.parent_id=parent.id;

I missed a "group by parent.name" above.
 

Sean



Re: Count the children

From
Michael Rowan
Date:
Thanks to all who replied.  I am an absolute newbie here.

Of course, parent.name is not unique, so I use parent.id instead and
all is well.  Except that I need to return columns from the parent
table such as name, phone etc.

I assume this can be done using a subquery, but have no idea how it
should be.

Any offers?

Mike
On 19/08/2009, at 12:14 AM, Mark Styles wrote:

> On Wed, Aug 19, 2009 at 12:04:13AM +0930, Michael Rowan wrote:
>> I need what sounds like a very simple query - but I'm darned if I can
>> figure it out.
>>
>> I have two tables, parent and child related by child.parent_id =
>> parent.id
>> The query should return, for each parent row, parent.name and
>> number of
>> child rows
>>
>> Can someone enlighten me, please?
>
> Assuming parent.name is unique:
>
> SELECT parent.name, count(*)
> FROM   parent, child
> WHERE  parent.key = child.parent_key
> GROUP BY parent.name;
>
> --
> Mark
> http://www.lambic.co.uk
>

Michael Rowan
mike.rowan@internode.on.net

11 Kingscote Street
ALBERTON
South Australia 5014

tel 618 8240 3993