mysql-nested select-如何从第一个表中选择值?

jgzswidk  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(271)

**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

我想知道是否可以从最后一张表(表doctordecision)中得到患者和医生的姓名?怎样才能成为一个选择呢?我正在尝试进行一些连接,但不确定是否可以从第一个表中获取值。
示例-如何知道上一个表中ResulteMamid=1的医生姓名、患者姓名和检查姓名?

lbsnaicq

lbsnaicq1#

此查询应提供所需的信息:

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

输出(用于示例数据)

Name    Name    ExamName
Mary    Jane    Blood Exam

sqlfiddle演示

相关问题