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?
mysql sql database select
add a comment |
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?
mysql sql database select
add a comment |
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?
mysql sql database select
**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
mysql sql database select
asked Nov 10 at 21:52
Zoro-Zen
196
196
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 10 at 22:00
Nick
19.1k41433
19.1k41433
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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