Select Parent Record With All Children in SQL

后端 未结 3 708
眼角桃花
眼角桃花 2021-02-15 11:42

Let\'s say I have two tables, \"Parent\" and \"Child\". Parent-to-Child is a many:many relationship, implemented through a standard cross-referencing table.

I want to fi

3条回答
  •  旧巷少年郎
    2021-02-15 12:05

    Here's an answer.

    SQL query: Simulating an "AND" over several rows instead of sub-querying

    And here's a specific application of that to this problem.

    SELECT * FROM Parents
    WHERE ParentId in
    (
      SELECT ParentId FROM ChildParent
      WHERE ChildId in
      (
        SELECT ChildId FROM Child
        WHERE ChildName in ('Charlie', 'David')
      )
      GROUP BY ParentId
      HAVING COUNT(*) = 2
    )
    

提交回复
热议问题