简体   繁体   English

如何找到具有相同属性的sql记录?

[英]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.

相关问题 SQL.如何查找字符串值几乎相同的记录? - SQL .How to find records with almost identical string values? 如何在 Access VBA 中运行追加查询以根据表单字段中的数量创建一定数量的相同记录? - How do I run an append query in Access VBA to create a certain number of identical records based on a quantity in a field on the form? 如何在SQL表中找到重复记录 - How do I find repeating records In a SQL table 如何在数据库中找到与键相关联且不包含任何特定值的记录? - How do I find records in a database that associate on on key and don't include a certain value on any of them? 如何查找自某个日期以来不同记录的计数(例如:2018 年至今)? - How do I find the count of distinct records since a certain date (eg: 2018 till current day)? 如何检查SQL记录(包括空格)是否相同? - How to check if SQL records are identical including spaces? 如何防止将相同的记录添加到SQL数据库 - How to prevent adding identical records to SQL database Oracle SQL-如何顺序遍历此结果集以除去某些记录? - Oracle SQL - How do I sequentially iterate through this result set to remove certain records? 如何使SQL返回相同的数据 - How Do I Make SQL Return Identical Data 比较两个SQL相同的表以查找丢失的记录 - Compare Two SQL identical Table to find missing records
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM