Click to See Complete Forum and Search --> : Does the parent row have a child?


BarryG79
02-26-2008, 09:18 AM
Hi,

I have a parent and child table. The child table has a column which contains the related primary key value from the parent. Some rows in the parent have no related child rows, while some have multiple child rows.

I which to list all the rows in the parent, plus an additional row which states if this row has a child or not, via true and false values.

How might I do this?

Thanks,

Barry.

chazzy
02-26-2008, 10:25 AM
Do you mean column or row?

if you mean column, you could do something like this:

select
columns_from_parent,
a.cnt
from parent p
left join
(
select count(*) cnt
from child c
where c.parent_id = p.id
) a
where
p.id=some value

Technoroj
02-27-2008, 06:06 AM
you can learn sql from www.w3schools.com for beginners its good site.

BarryG79
02-28-2008, 06:22 AM
Thanks for your replies!

The following is working for my in sql server -

SELECT parentId,
(SELECT COUNT(parentId) AS Expr1
FROM child
WHERE (parent.parentId = parentId))
AS number_of_children
FROM parent

How might I change this so number_of_children is of type boolean, with 0 = FALSE and any other number = TRUE?

Thanks for your help,

Barry.

chazzy
02-28-2008, 06:40 AM
sql server doesn't have a dedicated boolean type. why not use your application logic to determine it?