PHP MySQL 3 tables Foreign Key Join data from 1 Table into the other 2












-2















I have 3 tables Order, CheckoutStatus, and Statuses. There is a foreign key in Order and CheckoutStatus that references the Statuses table.



I need to join CheckoutStatus to Order linked by the PO column and I need to join Statuses to Order and to CheckoutStatus.



Here is the data in the tables



Order table



`PO` = 123456
foreign key `Statuses_id` = 2


CheckoutStatus



`PO` = 123456
foreign key `Statuses_id` = 0


Statuses



`id` 0 = Complete
`id` 2 = Completed


How do I write my SQL statement so that I can have a result like this.



Order
123456
Completed
CheckoutStatus
123456
Complete


This SQL statement I'm using does not display anything unless I remove one of the JOIN Statuses section of the statement.



SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = Order.Statuses_id JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO JOIN `Statuses` ON Statuses.id = CheckoutStatus.Statuses_id









share|improve this question





























    -2















    I have 3 tables Order, CheckoutStatus, and Statuses. There is a foreign key in Order and CheckoutStatus that references the Statuses table.



    I need to join CheckoutStatus to Order linked by the PO column and I need to join Statuses to Order and to CheckoutStatus.



    Here is the data in the tables



    Order table



    `PO` = 123456
    foreign key `Statuses_id` = 2


    CheckoutStatus



    `PO` = 123456
    foreign key `Statuses_id` = 0


    Statuses



    `id` 0 = Complete
    `id` 2 = Completed


    How do I write my SQL statement so that I can have a result like this.



    Order
    123456
    Completed
    CheckoutStatus
    123456
    Complete


    This SQL statement I'm using does not display anything unless I remove one of the JOIN Statuses section of the statement.



    SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = Order.Statuses_id JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO JOIN `Statuses` ON Statuses.id = CheckoutStatus.Statuses_id









    share|improve this question



























      -2












      -2








      -2








      I have 3 tables Order, CheckoutStatus, and Statuses. There is a foreign key in Order and CheckoutStatus that references the Statuses table.



      I need to join CheckoutStatus to Order linked by the PO column and I need to join Statuses to Order and to CheckoutStatus.



      Here is the data in the tables



      Order table



      `PO` = 123456
      foreign key `Statuses_id` = 2


      CheckoutStatus



      `PO` = 123456
      foreign key `Statuses_id` = 0


      Statuses



      `id` 0 = Complete
      `id` 2 = Completed


      How do I write my SQL statement so that I can have a result like this.



      Order
      123456
      Completed
      CheckoutStatus
      123456
      Complete


      This SQL statement I'm using does not display anything unless I remove one of the JOIN Statuses section of the statement.



      SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = Order.Statuses_id JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO JOIN `Statuses` ON Statuses.id = CheckoutStatus.Statuses_id









      share|improve this question
















      I have 3 tables Order, CheckoutStatus, and Statuses. There is a foreign key in Order and CheckoutStatus that references the Statuses table.



      I need to join CheckoutStatus to Order linked by the PO column and I need to join Statuses to Order and to CheckoutStatus.



      Here is the data in the tables



      Order table



      `PO` = 123456
      foreign key `Statuses_id` = 2


      CheckoutStatus



      `PO` = 123456
      foreign key `Statuses_id` = 0


      Statuses



      `id` 0 = Complete
      `id` 2 = Completed


      How do I write my SQL statement so that I can have a result like this.



      Order
      123456
      Completed
      CheckoutStatus
      123456
      Complete


      This SQL statement I'm using does not display anything unless I remove one of the JOIN Statuses section of the statement.



      SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = Order.Statuses_id JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO JOIN `Statuses` ON Statuses.id = CheckoutStatus.Statuses_id






      php mysql join foreign-keys






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 18:54









      GMB

      20.8k51028




      20.8k51028










      asked Jan 3 at 18:33









      MikeMike

      361217




      361217
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You have two JOINs on table Statuses. You need to use table aliases to distinguish the two relationships :



          SELECT 
          `Order`.PO,
          s1.Status,
          s2.Status
          FROM
          `Order`
          JOIN `Statuses` s1 ON s1.id = Order.Statuses_id
          JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO
          JOIN `Statuses` s2 ON s2.id = CheckoutStatus.Statuses_id





          share|improve this answer


























          • This is what I was looking for thanks

            – Mike
            Jan 3 at 19:15











          • besides using PHP to remove the Statuses_id from displaying in the output, is there something in the SQL statement I can add? Just want to display the PO number and the Status value of Complete and Completed as seen above in the question.

            – Mike
            Jan 3 at 20:30











          • @Mike : query updated. You might have t change the name of the column that hold the status value (I assumed « Statuses.value »).

            – GMB
            Jan 3 at 20:37











          • What is the reason to change the column name? The column names in the Statuses table are id and Status.

            – Mike
            Jan 3 at 20:41











          • @Mike... this information was not part of your original post so I had to assume. It is easy to change the query once you have the logic explained

            – GMB
            Jan 3 at 20:43



















          0














          this can get a little bit confusing you can split the query and on the basis of its fetched result, you can fetch further more



                $sql4 = "SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = 
          Order.Statuses_id JOIN `CheckoutStatus` ON somthing"
          $result4 = mysqli_query($conn, $sql4);
          if (mysqli_num_rows($result4) > 0)
          {
          while($row3 = mysqli_fetch_assoc($result4))
          {
          $sql5='SELECT * FROM //the result of before joining other two '
          }
          }


          something like this






          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%2f54027910%2fphp-mysql-3-tables-foreign-key-join-data-from-1-table-into-the-other-2%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














            You have two JOINs on table Statuses. You need to use table aliases to distinguish the two relationships :



            SELECT 
            `Order`.PO,
            s1.Status,
            s2.Status
            FROM
            `Order`
            JOIN `Statuses` s1 ON s1.id = Order.Statuses_id
            JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO
            JOIN `Statuses` s2 ON s2.id = CheckoutStatus.Statuses_id





            share|improve this answer


























            • This is what I was looking for thanks

              – Mike
              Jan 3 at 19:15











            • besides using PHP to remove the Statuses_id from displaying in the output, is there something in the SQL statement I can add? Just want to display the PO number and the Status value of Complete and Completed as seen above in the question.

              – Mike
              Jan 3 at 20:30











            • @Mike : query updated. You might have t change the name of the column that hold the status value (I assumed « Statuses.value »).

              – GMB
              Jan 3 at 20:37











            • What is the reason to change the column name? The column names in the Statuses table are id and Status.

              – Mike
              Jan 3 at 20:41











            • @Mike... this information was not part of your original post so I had to assume. It is easy to change the query once you have the logic explained

              – GMB
              Jan 3 at 20:43
















            1














            You have two JOINs on table Statuses. You need to use table aliases to distinguish the two relationships :



            SELECT 
            `Order`.PO,
            s1.Status,
            s2.Status
            FROM
            `Order`
            JOIN `Statuses` s1 ON s1.id = Order.Statuses_id
            JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO
            JOIN `Statuses` s2 ON s2.id = CheckoutStatus.Statuses_id





            share|improve this answer


























            • This is what I was looking for thanks

              – Mike
              Jan 3 at 19:15











            • besides using PHP to remove the Statuses_id from displaying in the output, is there something in the SQL statement I can add? Just want to display the PO number and the Status value of Complete and Completed as seen above in the question.

              – Mike
              Jan 3 at 20:30











            • @Mike : query updated. You might have t change the name of the column that hold the status value (I assumed « Statuses.value »).

              – GMB
              Jan 3 at 20:37











            • What is the reason to change the column name? The column names in the Statuses table are id and Status.

              – Mike
              Jan 3 at 20:41











            • @Mike... this information was not part of your original post so I had to assume. It is easy to change the query once you have the logic explained

              – GMB
              Jan 3 at 20:43














            1












            1








            1







            You have two JOINs on table Statuses. You need to use table aliases to distinguish the two relationships :



            SELECT 
            `Order`.PO,
            s1.Status,
            s2.Status
            FROM
            `Order`
            JOIN `Statuses` s1 ON s1.id = Order.Statuses_id
            JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO
            JOIN `Statuses` s2 ON s2.id = CheckoutStatus.Statuses_id





            share|improve this answer















            You have two JOINs on table Statuses. You need to use table aliases to distinguish the two relationships :



            SELECT 
            `Order`.PO,
            s1.Status,
            s2.Status
            FROM
            `Order`
            JOIN `Statuses` s1 ON s1.id = Order.Statuses_id
            JOIN `CheckoutStatus` ON Order.PO = CheckoutStatus.PO
            JOIN `Statuses` s2 ON s2.id = CheckoutStatus.Statuses_id






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 3 at 20:42

























            answered Jan 3 at 18:49









            GMBGMB

            20.8k51028




            20.8k51028













            • This is what I was looking for thanks

              – Mike
              Jan 3 at 19:15











            • besides using PHP to remove the Statuses_id from displaying in the output, is there something in the SQL statement I can add? Just want to display the PO number and the Status value of Complete and Completed as seen above in the question.

              – Mike
              Jan 3 at 20:30











            • @Mike : query updated. You might have t change the name of the column that hold the status value (I assumed « Statuses.value »).

              – GMB
              Jan 3 at 20:37











            • What is the reason to change the column name? The column names in the Statuses table are id and Status.

              – Mike
              Jan 3 at 20:41











            • @Mike... this information was not part of your original post so I had to assume. It is easy to change the query once you have the logic explained

              – GMB
              Jan 3 at 20:43



















            • This is what I was looking for thanks

              – Mike
              Jan 3 at 19:15











            • besides using PHP to remove the Statuses_id from displaying in the output, is there something in the SQL statement I can add? Just want to display the PO number and the Status value of Complete and Completed as seen above in the question.

              – Mike
              Jan 3 at 20:30











            • @Mike : query updated. You might have t change the name of the column that hold the status value (I assumed « Statuses.value »).

              – GMB
              Jan 3 at 20:37











            • What is the reason to change the column name? The column names in the Statuses table are id and Status.

              – Mike
              Jan 3 at 20:41











            • @Mike... this information was not part of your original post so I had to assume. It is easy to change the query once you have the logic explained

              – GMB
              Jan 3 at 20:43

















            This is what I was looking for thanks

            – Mike
            Jan 3 at 19:15





            This is what I was looking for thanks

            – Mike
            Jan 3 at 19:15













            besides using PHP to remove the Statuses_id from displaying in the output, is there something in the SQL statement I can add? Just want to display the PO number and the Status value of Complete and Completed as seen above in the question.

            – Mike
            Jan 3 at 20:30





            besides using PHP to remove the Statuses_id from displaying in the output, is there something in the SQL statement I can add? Just want to display the PO number and the Status value of Complete and Completed as seen above in the question.

            – Mike
            Jan 3 at 20:30













            @Mike : query updated. You might have t change the name of the column that hold the status value (I assumed « Statuses.value »).

            – GMB
            Jan 3 at 20:37





            @Mike : query updated. You might have t change the name of the column that hold the status value (I assumed « Statuses.value »).

            – GMB
            Jan 3 at 20:37













            What is the reason to change the column name? The column names in the Statuses table are id and Status.

            – Mike
            Jan 3 at 20:41





            What is the reason to change the column name? The column names in the Statuses table are id and Status.

            – Mike
            Jan 3 at 20:41













            @Mike... this information was not part of your original post so I had to assume. It is easy to change the query once you have the logic explained

            – GMB
            Jan 3 at 20:43





            @Mike... this information was not part of your original post so I had to assume. It is easy to change the query once you have the logic explained

            – GMB
            Jan 3 at 20:43













            0














            this can get a little bit confusing you can split the query and on the basis of its fetched result, you can fetch further more



                  $sql4 = "SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = 
            Order.Statuses_id JOIN `CheckoutStatus` ON somthing"
            $result4 = mysqli_query($conn, $sql4);
            if (mysqli_num_rows($result4) > 0)
            {
            while($row3 = mysqli_fetch_assoc($result4))
            {
            $sql5='SELECT * FROM //the result of before joining other two '
            }
            }


            something like this






            share|improve this answer






























              0














              this can get a little bit confusing you can split the query and on the basis of its fetched result, you can fetch further more



                    $sql4 = "SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = 
              Order.Statuses_id JOIN `CheckoutStatus` ON somthing"
              $result4 = mysqli_query($conn, $sql4);
              if (mysqli_num_rows($result4) > 0)
              {
              while($row3 = mysqli_fetch_assoc($result4))
              {
              $sql5='SELECT * FROM //the result of before joining other two '
              }
              }


              something like this






              share|improve this answer




























                0












                0








                0







                this can get a little bit confusing you can split the query and on the basis of its fetched result, you can fetch further more



                      $sql4 = "SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = 
                Order.Statuses_id JOIN `CheckoutStatus` ON somthing"
                $result4 = mysqli_query($conn, $sql4);
                if (mysqli_num_rows($result4) > 0)
                {
                while($row3 = mysqli_fetch_assoc($result4))
                {
                $sql5='SELECT * FROM //the result of before joining other two '
                }
                }


                something like this






                share|improve this answer















                this can get a little bit confusing you can split the query and on the basis of its fetched result, you can fetch further more



                      $sql4 = "SELECT * FROM `Order` JOIN `Statuses` ON Statuses.id = 
                Order.Statuses_id JOIN `CheckoutStatus` ON somthing"
                $result4 = mysqli_query($conn, $sql4);
                if (mysqli_num_rows($result4) > 0)
                {
                while($row3 = mysqli_fetch_assoc($result4))
                {
                $sql5='SELECT * FROM //the result of before joining other two '
                }
                }


                something like this







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 3 at 18:52

























                answered Jan 3 at 18:47









                KashanKashan

                101111




                101111






























                    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%2f54027910%2fphp-mysql-3-tables-foreign-key-join-data-from-1-table-into-the-other-2%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

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas