MySql - Nested Select - How to select the value from the first tables?











up vote
0
down vote

favorite












**Table Doctor**       
*ID Name Other Value*
1 Jane X
2 John Y
3 Jame Z

**Table Patient**
*ID Name Other Value*
1 Mary A
2 Mark B
3 Mel C

**Table Appointment**
*ID PatientID DoctorID OtherValue*
1 1 1 X
2 3 2 Y

**Table Exam**
*ID ExamName*
1 Blood Exam
2 Pregnant Exam

**Table RequestExam**
*ID AppointmentID ExamID*
1 1 1
2 2 2

**Table ResultExam**
*ID RequestExamID OtherValues*
1 1 XYZA
2 2 ABCD

**Table DoctorDecision**
*ID ResultExamID OtherValues*
1 1 Qwerty
2 2 Asdfgh


I would like to know If from the last table (Table DoctorDecision) can I get the patient and doctor names? How would be a select to make it? I'm trying to make some joins, but not sure If is possible to get the values from the first tables.



Example - How I would know the name of the doctor, patient name and exam name who has the ResultExamID = 1 in the last table?










share|improve this question


























    up vote
    0
    down vote

    favorite












    **Table Doctor**       
    *ID Name Other Value*
    1 Jane X
    2 John Y
    3 Jame Z

    **Table Patient**
    *ID Name Other Value*
    1 Mary A
    2 Mark B
    3 Mel C

    **Table Appointment**
    *ID PatientID DoctorID OtherValue*
    1 1 1 X
    2 3 2 Y

    **Table Exam**
    *ID ExamName*
    1 Blood Exam
    2 Pregnant Exam

    **Table RequestExam**
    *ID AppointmentID ExamID*
    1 1 1
    2 2 2

    **Table ResultExam**
    *ID RequestExamID OtherValues*
    1 1 XYZA
    2 2 ABCD

    **Table DoctorDecision**
    *ID ResultExamID OtherValues*
    1 1 Qwerty
    2 2 Asdfgh


    I would like to know If from the last table (Table DoctorDecision) can I get the patient and doctor names? How would be a select to make it? I'm trying to make some joins, but not sure If is possible to get the values from the first tables.



    Example - How I would know the name of the doctor, patient name and exam name who has the ResultExamID = 1 in the last table?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      **Table Doctor**       
      *ID Name Other Value*
      1 Jane X
      2 John Y
      3 Jame Z

      **Table Patient**
      *ID Name Other Value*
      1 Mary A
      2 Mark B
      3 Mel C

      **Table Appointment**
      *ID PatientID DoctorID OtherValue*
      1 1 1 X
      2 3 2 Y

      **Table Exam**
      *ID ExamName*
      1 Blood Exam
      2 Pregnant Exam

      **Table RequestExam**
      *ID AppointmentID ExamID*
      1 1 1
      2 2 2

      **Table ResultExam**
      *ID RequestExamID OtherValues*
      1 1 XYZA
      2 2 ABCD

      **Table DoctorDecision**
      *ID ResultExamID OtherValues*
      1 1 Qwerty
      2 2 Asdfgh


      I would like to know If from the last table (Table DoctorDecision) can I get the patient and doctor names? How would be a select to make it? I'm trying to make some joins, but not sure If is possible to get the values from the first tables.



      Example - How I would know the name of the doctor, patient name and exam name who has the ResultExamID = 1 in the last table?










      share|improve this question













      **Table Doctor**       
      *ID Name Other Value*
      1 Jane X
      2 John Y
      3 Jame Z

      **Table Patient**
      *ID Name Other Value*
      1 Mary A
      2 Mark B
      3 Mel C

      **Table Appointment**
      *ID PatientID DoctorID OtherValue*
      1 1 1 X
      2 3 2 Y

      **Table Exam**
      *ID ExamName*
      1 Blood Exam
      2 Pregnant Exam

      **Table RequestExam**
      *ID AppointmentID ExamID*
      1 1 1
      2 2 2

      **Table ResultExam**
      *ID RequestExamID OtherValues*
      1 1 XYZA
      2 2 ABCD

      **Table DoctorDecision**
      *ID ResultExamID OtherValues*
      1 1 Qwerty
      2 2 Asdfgh


      I would like to know If from the last table (Table DoctorDecision) can I get the patient and doctor names? How would be a select to make it? I'm trying to make some joins, but not sure If is possible to get the values from the first tables.



      Example - How I would know the name of the doctor, patient name and exam name who has the ResultExamID = 1 in the last table?







      mysql sql database select






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 21:52









      Zoro-Zen

      196




      196
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          This query should give you the information you want:



          SELECT p.Name, d.Name, e.ExamName
          FROM DoctorDecision dd
          JOIN ResultExam re ON re.ID = dd.ResultExamID
          JOIN RequestExam qe ON qe.ID = re.RequestExamID
          JOIN Exam e ON e.ID = qe.ExamID
          JOIN Appointment a ON a.ID = qe.AppointmentID
          JOIN Patient p ON p.ID = a.PatientID
          JOIN Doctor d ON d.ID = a.DoctorID
          WHERE dd.ResultExamID = 1


          Output (for your sample data)



          Name    Name    ExamName
          Mary Jane Blood Exam


          Demo on SQLFiddle






          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',
            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%2f53243769%2fmysql-nested-select-how-to-select-the-value-from-the-first-tables%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            This query should give you the information you want:



            SELECT p.Name, d.Name, e.ExamName
            FROM DoctorDecision dd
            JOIN ResultExam re ON re.ID = dd.ResultExamID
            JOIN RequestExam qe ON qe.ID = re.RequestExamID
            JOIN Exam e ON e.ID = qe.ExamID
            JOIN Appointment a ON a.ID = qe.AppointmentID
            JOIN Patient p ON p.ID = a.PatientID
            JOIN Doctor d ON d.ID = a.DoctorID
            WHERE dd.ResultExamID = 1


            Output (for your sample data)



            Name    Name    ExamName
            Mary Jane Blood Exam


            Demo on SQLFiddle






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              This query should give you the information you want:



              SELECT p.Name, d.Name, e.ExamName
              FROM DoctorDecision dd
              JOIN ResultExam re ON re.ID = dd.ResultExamID
              JOIN RequestExam qe ON qe.ID = re.RequestExamID
              JOIN Exam e ON e.ID = qe.ExamID
              JOIN Appointment a ON a.ID = qe.AppointmentID
              JOIN Patient p ON p.ID = a.PatientID
              JOIN Doctor d ON d.ID = a.DoctorID
              WHERE dd.ResultExamID = 1


              Output (for your sample data)



              Name    Name    ExamName
              Mary Jane Blood Exam


              Demo on SQLFiddle






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                This query should give you the information you want:



                SELECT p.Name, d.Name, e.ExamName
                FROM DoctorDecision dd
                JOIN ResultExam re ON re.ID = dd.ResultExamID
                JOIN RequestExam qe ON qe.ID = re.RequestExamID
                JOIN Exam e ON e.ID = qe.ExamID
                JOIN Appointment a ON a.ID = qe.AppointmentID
                JOIN Patient p ON p.ID = a.PatientID
                JOIN Doctor d ON d.ID = a.DoctorID
                WHERE dd.ResultExamID = 1


                Output (for your sample data)



                Name    Name    ExamName
                Mary Jane Blood Exam


                Demo on SQLFiddle






                share|improve this answer












                This query should give you the information you want:



                SELECT p.Name, d.Name, e.ExamName
                FROM DoctorDecision dd
                JOIN ResultExam re ON re.ID = dd.ResultExamID
                JOIN RequestExam qe ON qe.ID = re.RequestExamID
                JOIN Exam e ON e.ID = qe.ExamID
                JOIN Appointment a ON a.ID = qe.AppointmentID
                JOIN Patient p ON p.ID = a.PatientID
                JOIN Doctor d ON d.ID = a.DoctorID
                WHERE dd.ResultExamID = 1


                Output (for your sample data)



                Name    Name    ExamName
                Mary Jane Blood Exam


                Demo on SQLFiddle







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 22:00









                Nick

                19.1k41433




                19.1k41433






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243769%2fmysql-nested-select-how-to-select-the-value-from-the-first-tables%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

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation