Comparative select on the same table (mysql)












-1















I'm really newbie with mysql and i'm tryin to do a simple select, but I can't figure it out.



Thats what I have:



I got this in a Table named Control:
| CODE | OFFICE |
| 1 | usa |
| 2 | usa |
| 3 | usa |
| 4 | usa |
| 5 | usa |
| 1 | china |
| 3 | china |
| 4 | china |

And I need get this:

| CODE | OFFICE |
| 2 | usa |
| 5 | usa |


Then, SELECT code,office WHERE the codes still aren't registered with office = china.



I have to make a self join or something like that or use GROUP BY statement ?
I'm stuck here... I really would appreciate any help.










share|improve this question





























    -1















    I'm really newbie with mysql and i'm tryin to do a simple select, but I can't figure it out.



    Thats what I have:



    I got this in a Table named Control:
    | CODE | OFFICE |
    | 1 | usa |
    | 2 | usa |
    | 3 | usa |
    | 4 | usa |
    | 5 | usa |
    | 1 | china |
    | 3 | china |
    | 4 | china |

    And I need get this:

    | CODE | OFFICE |
    | 2 | usa |
    | 5 | usa |


    Then, SELECT code,office WHERE the codes still aren't registered with office = china.



    I have to make a self join or something like that or use GROUP BY statement ?
    I'm stuck here... I really would appreciate any help.










    share|improve this question



























      -1












      -1








      -1








      I'm really newbie with mysql and i'm tryin to do a simple select, but I can't figure it out.



      Thats what I have:



      I got this in a Table named Control:
      | CODE | OFFICE |
      | 1 | usa |
      | 2 | usa |
      | 3 | usa |
      | 4 | usa |
      | 5 | usa |
      | 1 | china |
      | 3 | china |
      | 4 | china |

      And I need get this:

      | CODE | OFFICE |
      | 2 | usa |
      | 5 | usa |


      Then, SELECT code,office WHERE the codes still aren't registered with office = china.



      I have to make a self join or something like that or use GROUP BY statement ?
      I'm stuck here... I really would appreciate any help.










      share|improve this question
















      I'm really newbie with mysql and i'm tryin to do a simple select, but I can't figure it out.



      Thats what I have:



      I got this in a Table named Control:
      | CODE | OFFICE |
      | 1 | usa |
      | 2 | usa |
      | 3 | usa |
      | 4 | usa |
      | 5 | usa |
      | 1 | china |
      | 3 | china |
      | 4 | china |

      And I need get this:

      | CODE | OFFICE |
      | 2 | usa |
      | 5 | usa |


      Then, SELECT code,office WHERE the codes still aren't registered with office = china.



      I have to make a self join or something like that or use GROUP BY statement ?
      I'm stuck here... I really would appreciate any help.







      mysql select statements






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 16:48









      Madhur Bhaiya

      19.6k62236




      19.6k62236










      asked Nov 14 '18 at 23:21









      Irvıng Ngr'Irvıng Ngr'

      396




      396
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I guess this is working



           create table Test(id integer, code integer, office varchar(100));

          insert into Test(id, code, office) values(1, 1, "usa"),(2, 2, "usa"),(3, 3, "usa"),(4, 4, "usa"),(5, 5, "usa"),(6, 1, "china"),(7, 3, "china"),(8, 4, "china");

          select * from Test;

          Select * from Test where code NOT IN (select code from Test where office = "china");


          You have use sub query.






          share|improve this answer
























          • Thanks so much @Prashant , that worked!

            – Irvıng Ngr'
            Nov 15 '18 at 18:02



















          1














          You can do a "Self-Left-Join" on Code, and consider only those rows where no match was found in the Right side, i.e., right side value IS NULL



          SELECT 
          tleft.*
          FROM Control AS tleft
          LEFT JOIN Control AS tright
          ON tright.Code = tleft.Code AND
          tright.Office = 'china'
          WHERE tleft.Office = 'usa' AND
          tright.Code IS NULL -- this filters no matching code found in china





          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%2f53310244%2fcomparative-select-on-the-same-table-mysql%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









            1














            I guess this is working



             create table Test(id integer, code integer, office varchar(100));

            insert into Test(id, code, office) values(1, 1, "usa"),(2, 2, "usa"),(3, 3, "usa"),(4, 4, "usa"),(5, 5, "usa"),(6, 1, "china"),(7, 3, "china"),(8, 4, "china");

            select * from Test;

            Select * from Test where code NOT IN (select code from Test where office = "china");


            You have use sub query.






            share|improve this answer
























            • Thanks so much @Prashant , that worked!

              – Irvıng Ngr'
              Nov 15 '18 at 18:02
















            1














            I guess this is working



             create table Test(id integer, code integer, office varchar(100));

            insert into Test(id, code, office) values(1, 1, "usa"),(2, 2, "usa"),(3, 3, "usa"),(4, 4, "usa"),(5, 5, "usa"),(6, 1, "china"),(7, 3, "china"),(8, 4, "china");

            select * from Test;

            Select * from Test where code NOT IN (select code from Test where office = "china");


            You have use sub query.






            share|improve this answer
























            • Thanks so much @Prashant , that worked!

              – Irvıng Ngr'
              Nov 15 '18 at 18:02














            1












            1








            1







            I guess this is working



             create table Test(id integer, code integer, office varchar(100));

            insert into Test(id, code, office) values(1, 1, "usa"),(2, 2, "usa"),(3, 3, "usa"),(4, 4, "usa"),(5, 5, "usa"),(6, 1, "china"),(7, 3, "china"),(8, 4, "china");

            select * from Test;

            Select * from Test where code NOT IN (select code from Test where office = "china");


            You have use sub query.






            share|improve this answer













            I guess this is working



             create table Test(id integer, code integer, office varchar(100));

            insert into Test(id, code, office) values(1, 1, "usa"),(2, 2, "usa"),(3, 3, "usa"),(4, 4, "usa"),(5, 5, "usa"),(6, 1, "china"),(7, 3, "china"),(8, 4, "china");

            select * from Test;

            Select * from Test where code NOT IN (select code from Test where office = "china");


            You have use sub query.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 6:26









            Prashant Deshmukh.....Prashant Deshmukh.....

            68658




            68658













            • Thanks so much @Prashant , that worked!

              – Irvıng Ngr'
              Nov 15 '18 at 18:02



















            • Thanks so much @Prashant , that worked!

              – Irvıng Ngr'
              Nov 15 '18 at 18:02

















            Thanks so much @Prashant , that worked!

            – Irvıng Ngr'
            Nov 15 '18 at 18:02





            Thanks so much @Prashant , that worked!

            – Irvıng Ngr'
            Nov 15 '18 at 18:02













            1














            You can do a "Self-Left-Join" on Code, and consider only those rows where no match was found in the Right side, i.e., right side value IS NULL



            SELECT 
            tleft.*
            FROM Control AS tleft
            LEFT JOIN Control AS tright
            ON tright.Code = tleft.Code AND
            tright.Office = 'china'
            WHERE tleft.Office = 'usa' AND
            tright.Code IS NULL -- this filters no matching code found in china





            share|improve this answer




























              1














              You can do a "Self-Left-Join" on Code, and consider only those rows where no match was found in the Right side, i.e., right side value IS NULL



              SELECT 
              tleft.*
              FROM Control AS tleft
              LEFT JOIN Control AS tright
              ON tright.Code = tleft.Code AND
              tright.Office = 'china'
              WHERE tleft.Office = 'usa' AND
              tright.Code IS NULL -- this filters no matching code found in china





              share|improve this answer


























                1












                1








                1







                You can do a "Self-Left-Join" on Code, and consider only those rows where no match was found in the Right side, i.e., right side value IS NULL



                SELECT 
                tleft.*
                FROM Control AS tleft
                LEFT JOIN Control AS tright
                ON tright.Code = tleft.Code AND
                tright.Office = 'china'
                WHERE tleft.Office = 'usa' AND
                tright.Code IS NULL -- this filters no matching code found in china





                share|improve this answer













                You can do a "Self-Left-Join" on Code, and consider only those rows where no match was found in the Right side, i.e., right side value IS NULL



                SELECT 
                tleft.*
                FROM Control AS tleft
                LEFT JOIN Control AS tright
                ON tright.Code = tleft.Code AND
                tright.Office = 'china'
                WHERE tleft.Office = 'usa' AND
                tright.Code IS NULL -- this filters no matching code found in china






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 16:53









                Madhur BhaiyaMadhur Bhaiya

                19.6k62236




                19.6k62236






























                    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%2f53310244%2fcomparative-select-on-the-same-table-mysql%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