【问题标题】:Joins and Nested Queries - multiple tables (4+)连接和嵌套查询 - 多个表 (4+)
【发布时间】:2015-04-29 02:22:18
【问题描述】:

RDMBS:甲骨文 嵌套查询是一项要求。

我正在尝试收集 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';

任何更正或建议将不胜感激。

【问题讨论】:

  • 你遇到了什么错误?
  • 第 3 行出现错误:ORA-00904:“疾病”。“名称”:标识符无效

标签: sql oracle join nested-queries


【解决方案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';

【讨论】:

  • 有没有办法用嵌套查询来写?
  • 我刚刚测试了一下,说第5行有错误,并且那个divided.appointmentid是一个无效的标识符。
  • @Shandep - 诊断出的表是否包含appointmentid 列??
  • 它确实 - 我解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 2021-02-21
  • 2020-02-27
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多