[英]How do I find sql records with certain identical attributes?
I have a table called parents
: 我有一张叫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. 我需要获得一个表,其中包含所有具有相同父项的孩子的childID
。
I tried 我试过了
SELECT childID
FROM parents
GROUP BY motherID, fatherID
but that also is giving me children who do not have a sibling. 但这也给了我没有兄弟姐妹的孩子。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.