RDMBS:Oracle 嵌套查询是一项要求。
我试图收集2014年3月预约的所有患者,并展示他们看到的医生,以及他们被诊断出患有什么。然后显示约会ID,患者姓名,年龄,性别和电话,并显示医生姓名和电话。我能够完成大部分工作,直到我问到患者被诊断出患有什么。
此代码允许我访问2014年3月的患者和医生记录:
Select Appointment.appointmentid, patient.surname ||','|| patient.given as Patient, trunc(((sysdate-patient.dob)/365),0) as Patient_Age, patient.phonehome as Patient_Contact, doctor.Surname ||','|| doctor.given as Doctor, doctor.phone as Doctor_Contact
from doctor, patient, appointment
where doctor.doctorid=appointment.doctorid
and patient.patientid=appointment.patientid
and extract(month from dateofappointment) = '03'
and extract(year from dateofappointment) = '2014';
但是一旦我在嵌套查询中进行诊断,我就会收到错误。
代码:
Select Appointment.appointmentid, patient.surname ||','|| patient.given as Patient, trunc(((sysdate-patient.dob)/365),0) as Patient_Age, patient.phonehome as Patient_Contact, disease.name as Diagnosis, doctor.Surname ||','|| doctor.given as Doctor, doctor.phone as Doctor_Contact
from doctor, patient, appointment
where disease.name in (select disease.name
from disease
where disease.diseaseid=diagnosed.diseaseid
and diagnosed.appointmentid=appointment.appointmentid)
and doctor.doctorid=appointment.doctorid
and patient.patientid=appointment.patientid
and extract(month from dateofappointment) = '03'
and extract(year from dateofappointment) = '2014';
任何更正或建议将不胜感激。
答案 0 :(得分:1)
您没有引用disease
表。您可以像这样重写查询:
Select
Appointment.appointmentid,
patient.surname ||','|| patient.given as Patient,
trunc(((sysdate-patient.dob)/365),0) as Patient_Age,
patient.phonehome as Patient_Contact,
disease.name as Diagnosis,
doctor.Surname ||','|| doctor.given as Doctor,
doctor.phone as Doctor_Contact
from doctor
inner join appointment on doctor.doctorid = appointment.doctorid
inner join patient on patient.patientid = appointment.patientid
inner join diagonsed on diagnosed.appointmentid = appointment.appointmentid
inner join disease on disease.diseaseid = diagnosed.diseaseid
where extract(month from dateofappointment) = '03'
and extract(year from dateofappointment) = '2014';
或者,使用相同的嵌套查询,您可以像这样重写它:
Select
Appointment.appointmentid,
patient.surname ||','|| patient.given as Patient,
trunc(((sysdate-patient.dob)/365),0) as Patient_Age,
patient.phonehome as Patient_Contact,
disease.name as Diagnosis,
doctor.Surname ||','|| doctor.given as Doctor,
doctor.phone as Doctor_Contact
from doctor, appointment, patient, diagnosed, disease
where doctor.doctorid = appointment.doctorid
and appointment.patientid = patient.patientid
and diagnosed.appointmentid=appointment.appointmentid
and disease.diseaseid=diagnosed.diseaseid
and disease.name in (select disease.name
from disease
where disease.diseaseid=diagnosed.diseaseid)
and extract(month from dateofappointment) = '03'
and extract(year from dateofappointment) = '2014';