SELECT JOIN WHERE a record does not exist











up vote
0
down vote

favorite












I have 2 tables



id      name   type
1 aa driver
2 bb cyclist
3 cc runner

parent_id key value
1 mobile 00299029
2 mobile 008772
2 active 1
3 mobile 09887
3 active 0


I need to get the record 1,aa,driver, the one that has not a record with value 'active' in the second table.
My last try was something like this, but I'n not sure to be even a bit close to what I need, my result is always 0 records



SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND NOT EXISTS (
SELECT * FROM table2
WHERE key = 'active'
)









share|improve this question




























    up vote
    0
    down vote

    favorite












    I have 2 tables



    id      name   type
    1 aa driver
    2 bb cyclist
    3 cc runner

    parent_id key value
    1 mobile 00299029
    2 mobile 008772
    2 active 1
    3 mobile 09887
    3 active 0


    I need to get the record 1,aa,driver, the one that has not a record with value 'active' in the second table.
    My last try was something like this, but I'n not sure to be even a bit close to what I need, my result is always 0 records



    SELECT t1.name as name
    FROM table1 as t1
    JOIN table2 as t2 ON t1.id = t2.parent_id
    AND NOT EXISTS (
    SELECT * FROM table2
    WHERE key = 'active'
    )









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have 2 tables



      id      name   type
      1 aa driver
      2 bb cyclist
      3 cc runner

      parent_id key value
      1 mobile 00299029
      2 mobile 008772
      2 active 1
      3 mobile 09887
      3 active 0


      I need to get the record 1,aa,driver, the one that has not a record with value 'active' in the second table.
      My last try was something like this, but I'n not sure to be even a bit close to what I need, my result is always 0 records



      SELECT t1.name as name
      FROM table1 as t1
      JOIN table2 as t2 ON t1.id = t2.parent_id
      AND NOT EXISTS (
      SELECT * FROM table2
      WHERE key = 'active'
      )









      share|improve this question















      I have 2 tables



      id      name   type
      1 aa driver
      2 bb cyclist
      3 cc runner

      parent_id key value
      1 mobile 00299029
      2 mobile 008772
      2 active 1
      3 mobile 09887
      3 active 0


      I need to get the record 1,aa,driver, the one that has not a record with value 'active' in the second table.
      My last try was something like this, but I'n not sure to be even a bit close to what I need, my result is always 0 records



      SELECT t1.name as name
      FROM table1 as t1
      JOIN table2 as t2 ON t1.id = t2.parent_id
      AND NOT EXISTS (
      SELECT * FROM table2
      WHERE key = 'active'
      )






      mysql sql join






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Rahul Neekhra

      607627




      607627










      asked yesterday









      ssstofff

      451111




      451111
























          7 Answers
          7






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Just a left join would do



              SELECT t1.name as name
          FROM table1 as t1
          LEFT JOIN table2 as t2
          ON t1.id = t2.parent_id
          AND t2.key = 'active'
          WHERE t2.key IS NULL





          share|improve this answer




























            up vote
            0
            down vote













            Try this (I think, not entirely sure I understand the linkage):



            SELECT t1.name as name
            FROM table1 as t1
            LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null


            A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.






            share|improve this answer




























              up vote
              0
              down vote













              This will work:



              SELECT t1.name as name
              FROM table1 as t1
              JOIN table2 as t2 ON t1.id = t2.parent_id
              AND t2.key not like '%active%'





              share|improve this answer




























                up vote
                0
                down vote













                You can use NOT EXISTS as follows:



                select t1.name from table1 t1 
                where not exists
                (
                select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
                )





                share|improve this answer




























                  up vote
                  0
                  down vote













                  Select id,name,type from table1 
                  where id Not in
                  (Select
                  parent_id from table2
                  group by parent_id
                  having key=
                  'active')



                  Better avoid join in this case I would say as subquery
                  would do good in this case.







                  share|improve this answer






























                    up vote
                    0
                    down vote














                    I think Your query is not working in this case, please try
                    this




                     SELECT name FROM table1 
                    JOIN table2 ON table1.id =
                    table2.parent_id where id not IN ( SELECT parent_id
                    FROM
                    table2 WHERE `key` = 'active' )





                    share|improve this answer










                    New contributor




                    user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.


















                    • Why you used join and subquery together I dont understand ?
                      – Himanshu Ahuja
                      yesterday










                    • I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
                      – user9639650
                      21 hours ago


















                    up vote
                    -1
                    down vote













                    SELECT t1.id,t1.name,t1.type,t2.key,t2.value
                    FROM table1 t1
                    JOIN table2 t2 ON t1.id = t2.parent_id
                    where t2.key <>'active'






                    share|improve this answer








                    New contributor




                    Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.


















                      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',
                      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%2f53416996%2fselect-join-where-a-record-does-not-exist%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      7 Answers
                      7






                      active

                      oldest

                      votes








                      7 Answers
                      7






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      0
                      down vote



                      accepted










                      Just a left join would do



                          SELECT t1.name as name
                      FROM table1 as t1
                      LEFT JOIN table2 as t2
                      ON t1.id = t2.parent_id
                      AND t2.key = 'active'
                      WHERE t2.key IS NULL





                      share|improve this answer

























                        up vote
                        0
                        down vote



                        accepted










                        Just a left join would do



                            SELECT t1.name as name
                        FROM table1 as t1
                        LEFT JOIN table2 as t2
                        ON t1.id = t2.parent_id
                        AND t2.key = 'active'
                        WHERE t2.key IS NULL





                        share|improve this answer























                          up vote
                          0
                          down vote



                          accepted







                          up vote
                          0
                          down vote



                          accepted






                          Just a left join would do



                              SELECT t1.name as name
                          FROM table1 as t1
                          LEFT JOIN table2 as t2
                          ON t1.id = t2.parent_id
                          AND t2.key = 'active'
                          WHERE t2.key IS NULL





                          share|improve this answer












                          Just a left join would do



                              SELECT t1.name as name
                          FROM table1 as t1
                          LEFT JOIN table2 as t2
                          ON t1.id = t2.parent_id
                          AND t2.key = 'active'
                          WHERE t2.key IS NULL






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered yesterday









                          George Joseph

                          45726




                          45726
























                              up vote
                              0
                              down vote













                              Try this (I think, not entirely sure I understand the linkage):



                              SELECT t1.name as name
                              FROM table1 as t1
                              LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null


                              A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.






                              share|improve this answer

























                                up vote
                                0
                                down vote













                                Try this (I think, not entirely sure I understand the linkage):



                                SELECT t1.name as name
                                FROM table1 as t1
                                LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null


                                A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.






                                share|improve this answer























                                  up vote
                                  0
                                  down vote










                                  up vote
                                  0
                                  down vote









                                  Try this (I think, not entirely sure I understand the linkage):



                                  SELECT t1.name as name
                                  FROM table1 as t1
                                  LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null


                                  A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.






                                  share|improve this answer












                                  Try this (I think, not entirely sure I understand the linkage):



                                  SELECT t1.name as name
                                  FROM table1 as t1
                                  LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null


                                  A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered yesterday









                                  Chris Thompson

                                  28.7k96896




                                  28.7k96896






















                                      up vote
                                      0
                                      down vote













                                      This will work:



                                      SELECT t1.name as name
                                      FROM table1 as t1
                                      JOIN table2 as t2 ON t1.id = t2.parent_id
                                      AND t2.key not like '%active%'





                                      share|improve this answer

























                                        up vote
                                        0
                                        down vote













                                        This will work:



                                        SELECT t1.name as name
                                        FROM table1 as t1
                                        JOIN table2 as t2 ON t1.id = t2.parent_id
                                        AND t2.key not like '%active%'





                                        share|improve this answer























                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          This will work:



                                          SELECT t1.name as name
                                          FROM table1 as t1
                                          JOIN table2 as t2 ON t1.id = t2.parent_id
                                          AND t2.key not like '%active%'





                                          share|improve this answer












                                          This will work:



                                          SELECT t1.name as name
                                          FROM table1 as t1
                                          JOIN table2 as t2 ON t1.id = t2.parent_id
                                          AND t2.key not like '%active%'






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered yesterday









                                          Vijesh

                                          12519




                                          12519






















                                              up vote
                                              0
                                              down vote













                                              You can use NOT EXISTS as follows:



                                              select t1.name from table1 t1 
                                              where not exists
                                              (
                                              select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
                                              )





                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote













                                                You can use NOT EXISTS as follows:



                                                select t1.name from table1 t1 
                                                where not exists
                                                (
                                                select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
                                                )





                                                share|improve this answer























                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  You can use NOT EXISTS as follows:



                                                  select t1.name from table1 t1 
                                                  where not exists
                                                  (
                                                  select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
                                                  )





                                                  share|improve this answer












                                                  You can use NOT EXISTS as follows:



                                                  select t1.name from table1 t1 
                                                  where not exists
                                                  (
                                                  select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
                                                  )






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered yesterday









                                                  isaace

                                                  2,5981415




                                                  2,5981415






















                                                      up vote
                                                      0
                                                      down vote













                                                      Select id,name,type from table1 
                                                      where id Not in
                                                      (Select
                                                      parent_id from table2
                                                      group by parent_id
                                                      having key=
                                                      'active')



                                                      Better avoid join in this case I would say as subquery
                                                      would do good in this case.







                                                      share|improve this answer



























                                                        up vote
                                                        0
                                                        down vote













                                                        Select id,name,type from table1 
                                                        where id Not in
                                                        (Select
                                                        parent_id from table2
                                                        group by parent_id
                                                        having key=
                                                        'active')



                                                        Better avoid join in this case I would say as subquery
                                                        would do good in this case.







                                                        share|improve this answer

























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          Select id,name,type from table1 
                                                          where id Not in
                                                          (Select
                                                          parent_id from table2
                                                          group by parent_id
                                                          having key=
                                                          'active')



                                                          Better avoid join in this case I would say as subquery
                                                          would do good in this case.







                                                          share|improve this answer














                                                          Select id,name,type from table1 
                                                          where id Not in
                                                          (Select
                                                          parent_id from table2
                                                          group by parent_id
                                                          having key=
                                                          'active')



                                                          Better avoid join in this case I would say as subquery
                                                          would do good in this case.








                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited yesterday

























                                                          answered yesterday









                                                          Himanshu Ahuja

                                                          22812




                                                          22812






















                                                              up vote
                                                              0
                                                              down vote














                                                              I think Your query is not working in this case, please try
                                                              this




                                                               SELECT name FROM table1 
                                                              JOIN table2 ON table1.id =
                                                              table2.parent_id where id not IN ( SELECT parent_id
                                                              FROM
                                                              table2 WHERE `key` = 'active' )





                                                              share|improve this answer










                                                              New contributor




                                                              user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.


















                                                              • Why you used join and subquery together I dont understand ?
                                                                – Himanshu Ahuja
                                                                yesterday










                                                              • I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
                                                                – user9639650
                                                                21 hours ago















                                                              up vote
                                                              0
                                                              down vote














                                                              I think Your query is not working in this case, please try
                                                              this




                                                               SELECT name FROM table1 
                                                              JOIN table2 ON table1.id =
                                                              table2.parent_id where id not IN ( SELECT parent_id
                                                              FROM
                                                              table2 WHERE `key` = 'active' )





                                                              share|improve this answer










                                                              New contributor




                                                              user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.


















                                                              • Why you used join and subquery together I dont understand ?
                                                                – Himanshu Ahuja
                                                                yesterday










                                                              • I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
                                                                – user9639650
                                                                21 hours ago













                                                              up vote
                                                              0
                                                              down vote










                                                              up vote
                                                              0
                                                              down vote










                                                              I think Your query is not working in this case, please try
                                                              this




                                                               SELECT name FROM table1 
                                                              JOIN table2 ON table1.id =
                                                              table2.parent_id where id not IN ( SELECT parent_id
                                                              FROM
                                                              table2 WHERE `key` = 'active' )





                                                              share|improve this answer










                                                              New contributor




                                                              user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.










                                                              I think Your query is not working in this case, please try
                                                              this




                                                               SELECT name FROM table1 
                                                              JOIN table2 ON table1.id =
                                                              table2.parent_id where id not IN ( SELECT parent_id
                                                              FROM
                                                              table2 WHERE `key` = 'active' )






                                                              share|improve this answer










                                                              New contributor




                                                              user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.









                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited yesterday









                                                              Himanshu Ahuja

                                                              22812




                                                              22812






                                                              New contributor




                                                              user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.









                                                              answered yesterday









                                                              user9639650

                                                              572




                                                              572




                                                              New contributor




                                                              user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.





                                                              New contributor





                                                              user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.






                                                              user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.












                                                              • Why you used join and subquery together I dont understand ?
                                                                – Himanshu Ahuja
                                                                yesterday










                                                              • I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
                                                                – user9639650
                                                                21 hours ago


















                                                              • Why you used join and subquery together I dont understand ?
                                                                – Himanshu Ahuja
                                                                yesterday










                                                              • I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
                                                                – user9639650
                                                                21 hours ago
















                                                              Why you used join and subquery together I dont understand ?
                                                              – Himanshu Ahuja
                                                              yesterday




                                                              Why you used join and subquery together I dont understand ?
                                                              – Himanshu Ahuja
                                                              yesterday












                                                              I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
                                                              – user9639650
                                                              21 hours ago




                                                              I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
                                                              – user9639650
                                                              21 hours ago










                                                              up vote
                                                              -1
                                                              down vote













                                                              SELECT t1.id,t1.name,t1.type,t2.key,t2.value
                                                              FROM table1 t1
                                                              JOIN table2 t2 ON t1.id = t2.parent_id
                                                              where t2.key <>'active'






                                                              share|improve this answer








                                                              New contributor




                                                              Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.






















                                                                up vote
                                                                -1
                                                                down vote













                                                                SELECT t1.id,t1.name,t1.type,t2.key,t2.value
                                                                FROM table1 t1
                                                                JOIN table2 t2 ON t1.id = t2.parent_id
                                                                where t2.key <>'active'






                                                                share|improve this answer








                                                                New contributor




                                                                Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                Check out our Code of Conduct.




















                                                                  up vote
                                                                  -1
                                                                  down vote










                                                                  up vote
                                                                  -1
                                                                  down vote









                                                                  SELECT t1.id,t1.name,t1.type,t2.key,t2.value
                                                                  FROM table1 t1
                                                                  JOIN table2 t2 ON t1.id = t2.parent_id
                                                                  where t2.key <>'active'






                                                                  share|improve this answer








                                                                  New contributor




                                                                  Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.









                                                                  SELECT t1.id,t1.name,t1.type,t2.key,t2.value
                                                                  FROM table1 t1
                                                                  JOIN table2 t2 ON t1.id = t2.parent_id
                                                                  where t2.key <>'active'







                                                                  share|improve this answer








                                                                  New contributor




                                                                  Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.









                                                                  share|improve this answer



                                                                  share|improve this answer






                                                                  New contributor




                                                                  Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.









                                                                  answered yesterday









                                                                  Md Abdul Mannan

                                                                  11




                                                                  11




                                                                  New contributor




                                                                  Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.





                                                                  New contributor





                                                                  Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.






                                                                  Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.






























                                                                       

                                                                      draft saved


                                                                      draft discarded



















































                                                                       


                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416996%2fselect-join-where-a-record-does-not-exist%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

                                                                      Catalogne

                                                                      Violoncelliste

                                                                      Héron pourpré