SQL: How to find all parents where parents have children with given id





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a question about a parent / child join table. I have a join table called parent_children. The table contains a parent_id and child_id. So like this:



parent_id  | child_id
1 1
1 2
1 3
2 1
2 3
3 1
3 4


What I want is to find all parents who have children in a certain list. So let's say the list contains 1 and 3 then I want parent_id 1 and 2. If the list contains 4 I want parent_id 3. How do I do this?










share|improve this question

























  • Child 1 has three parents?

    – jarlh
    Nov 16 '18 at 15:53


















0















I have a question about a parent / child join table. I have a join table called parent_children. The table contains a parent_id and child_id. So like this:



parent_id  | child_id
1 1
1 2
1 3
2 1
2 3
3 1
3 4


What I want is to find all parents who have children in a certain list. So let's say the list contains 1 and 3 then I want parent_id 1 and 2. If the list contains 4 I want parent_id 3. How do I do this?










share|improve this question

























  • Child 1 has three parents?

    – jarlh
    Nov 16 '18 at 15:53














0












0








0








I have a question about a parent / child join table. I have a join table called parent_children. The table contains a parent_id and child_id. So like this:



parent_id  | child_id
1 1
1 2
1 3
2 1
2 3
3 1
3 4


What I want is to find all parents who have children in a certain list. So let's say the list contains 1 and 3 then I want parent_id 1 and 2. If the list contains 4 I want parent_id 3. How do I do this?










share|improve this question
















I have a question about a parent / child join table. I have a join table called parent_children. The table contains a parent_id and child_id. So like this:



parent_id  | child_id
1 1
1 2
1 3
2 1
2 3
3 1
3 4


What I want is to find all parents who have children in a certain list. So let's say the list contains 1 and 3 then I want parent_id 1 and 2. If the list contains 4 I want parent_id 3. How do I do this?







sql join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 15:52









melpomene

62.5k55195




62.5k55195










asked Nov 16 '18 at 15:50









Martijn HiemstraMartijn Hiemstra

127211




127211













  • Child 1 has three parents?

    – jarlh
    Nov 16 '18 at 15:53



















  • Child 1 has three parents?

    – jarlh
    Nov 16 '18 at 15:53

















Child 1 has three parents?

– jarlh
Nov 16 '18 at 15:53





Child 1 has three parents?

– jarlh
Nov 16 '18 at 15:53












2 Answers
2






active

oldest

votes


















2














You can use group by, where, and having:



select parent_id
from t
where child_id in (1, 3)
group by parent_id
having count(*) = 2; -- "2" is the number of items in the list


This assumes no duplicate parent/child rows in the table. If this is possible, then use count(distinct child_id) = 2.






share|improve this answer
























  • This is brilliant. Thanks for the answer. I don't know how on earth you came up with that.

    – Martijn Hiemstra
    Nov 16 '18 at 19:00



















0














So you basically need to count the number of occurrences in the IN list of your input.
The following can help.



create table parent_children(parent_id int, child_id int)


insert into parent_children values(1,1)
insert into parent_children values(1,2)
insert into parent_children values(1,3)
insert into parent_children values(2,1)
insert into parent_children values(2,3)
insert into parent_children values(3,1)
insert into parent_children values(3,4)



with list_data
as(select *
from (values(1),(3))as t(x)
)
select a.x
from list_data a
left join parent_children b
on a.x=b.child_id
group by a.x
having count(*) = count(distinct b.parent_id)





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341238%2fsql-how-to-find-all-parents-where-parents-have-children-with-given-id%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can use group by, where, and having:



    select parent_id
    from t
    where child_id in (1, 3)
    group by parent_id
    having count(*) = 2; -- "2" is the number of items in the list


    This assumes no duplicate parent/child rows in the table. If this is possible, then use count(distinct child_id) = 2.






    share|improve this answer
























    • This is brilliant. Thanks for the answer. I don't know how on earth you came up with that.

      – Martijn Hiemstra
      Nov 16 '18 at 19:00
















    2














    You can use group by, where, and having:



    select parent_id
    from t
    where child_id in (1, 3)
    group by parent_id
    having count(*) = 2; -- "2" is the number of items in the list


    This assumes no duplicate parent/child rows in the table. If this is possible, then use count(distinct child_id) = 2.






    share|improve this answer
























    • This is brilliant. Thanks for the answer. I don't know how on earth you came up with that.

      – Martijn Hiemstra
      Nov 16 '18 at 19:00














    2












    2








    2







    You can use group by, where, and having:



    select parent_id
    from t
    where child_id in (1, 3)
    group by parent_id
    having count(*) = 2; -- "2" is the number of items in the list


    This assumes no duplicate parent/child rows in the table. If this is possible, then use count(distinct child_id) = 2.






    share|improve this answer













    You can use group by, where, and having:



    select parent_id
    from t
    where child_id in (1, 3)
    group by parent_id
    having count(*) = 2; -- "2" is the number of items in the list


    This assumes no duplicate parent/child rows in the table. If this is possible, then use count(distinct child_id) = 2.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 15:52









    Gordon LinoffGordon Linoff

    797k37318423




    797k37318423













    • This is brilliant. Thanks for the answer. I don't know how on earth you came up with that.

      – Martijn Hiemstra
      Nov 16 '18 at 19:00



















    • This is brilliant. Thanks for the answer. I don't know how on earth you came up with that.

      – Martijn Hiemstra
      Nov 16 '18 at 19:00

















    This is brilliant. Thanks for the answer. I don't know how on earth you came up with that.

    – Martijn Hiemstra
    Nov 16 '18 at 19:00





    This is brilliant. Thanks for the answer. I don't know how on earth you came up with that.

    – Martijn Hiemstra
    Nov 16 '18 at 19:00













    0














    So you basically need to count the number of occurrences in the IN list of your input.
    The following can help.



    create table parent_children(parent_id int, child_id int)


    insert into parent_children values(1,1)
    insert into parent_children values(1,2)
    insert into parent_children values(1,3)
    insert into parent_children values(2,1)
    insert into parent_children values(2,3)
    insert into parent_children values(3,1)
    insert into parent_children values(3,4)



    with list_data
    as(select *
    from (values(1),(3))as t(x)
    )
    select a.x
    from list_data a
    left join parent_children b
    on a.x=b.child_id
    group by a.x
    having count(*) = count(distinct b.parent_id)





    share|improve this answer




























      0














      So you basically need to count the number of occurrences in the IN list of your input.
      The following can help.



      create table parent_children(parent_id int, child_id int)


      insert into parent_children values(1,1)
      insert into parent_children values(1,2)
      insert into parent_children values(1,3)
      insert into parent_children values(2,1)
      insert into parent_children values(2,3)
      insert into parent_children values(3,1)
      insert into parent_children values(3,4)



      with list_data
      as(select *
      from (values(1),(3))as t(x)
      )
      select a.x
      from list_data a
      left join parent_children b
      on a.x=b.child_id
      group by a.x
      having count(*) = count(distinct b.parent_id)





      share|improve this answer


























        0












        0








        0







        So you basically need to count the number of occurrences in the IN list of your input.
        The following can help.



        create table parent_children(parent_id int, child_id int)


        insert into parent_children values(1,1)
        insert into parent_children values(1,2)
        insert into parent_children values(1,3)
        insert into parent_children values(2,1)
        insert into parent_children values(2,3)
        insert into parent_children values(3,1)
        insert into parent_children values(3,4)



        with list_data
        as(select *
        from (values(1),(3))as t(x)
        )
        select a.x
        from list_data a
        left join parent_children b
        on a.x=b.child_id
        group by a.x
        having count(*) = count(distinct b.parent_id)





        share|improve this answer













        So you basically need to count the number of occurrences in the IN list of your input.
        The following can help.



        create table parent_children(parent_id int, child_id int)


        insert into parent_children values(1,1)
        insert into parent_children values(1,2)
        insert into parent_children values(1,3)
        insert into parent_children values(2,1)
        insert into parent_children values(2,3)
        insert into parent_children values(3,1)
        insert into parent_children values(3,4)



        with list_data
        as(select *
        from (values(1),(3))as t(x)
        )
        select a.x
        from list_data a
        left join parent_children b
        on a.x=b.child_id
        group by a.x
        having count(*) = count(distinct b.parent_id)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 16:01









        George JosephGeorge Joseph

        1,590510




        1,590510






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341238%2fsql-how-to-find-all-parents-where-parents-have-children-with-given-id%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python