Reputation: 23322
I have a table called parents
:
________________________________
|childID | motherID | fatherID |
--------------------------------
| 2 | 1 | 100 |
| 3 | 2 | 101 |
| 4 | 2 | 101 |
...
I need to get a table returned with the childID
s of all the children who have the same parents.
I tried
SELECT childID
FROM parents
GROUP BY motherID, fatherID
but that also is giving me children who do not have a sibling.
Upvotes: 0
Views: 54
Reputation: 12837
SELECT
ChildId
FROM
parents p
JOIN
(
SELECT
motherID,
fatherID,
COUNT(*)
FROM
parents
GROUP BY
motherID,
fatherID
HAVING
COUNT(*) > 1
) motherFatherCount
ON p.MotherId = motherFatherCount.MotherID AND
p.FatherId = motherFatherCount.FatherId
Upvotes: 2