从四个表格制作报告

时间:2013-01-17 09:41:08

标签: sql oracle oracle11g sqlplus

我在Oracle APEX工作。我正在通过以下四个表格Patient History Junction and Disease制作报告,但无法制作报告。

我想要SELECT

  来自患者表的

Pat_Name,Pat_Age`

  

Treated_By,来自历史记录表的Sys_Date

  

疾病表中的Dis_Name

Junction之间有History and Disease表。以下是上述方案的图表。

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要JOIN每个表,类似于:

select p.pat_name,
  p.pat_age,
  h.treated_by,
  h.sys_date,
  d.dis_name
from patient p
inner join history h
  on p.pat_id = h.pat_id
  and p.app_id = h.app_id
left join junction j
  on p.pat_id = j.pat_id
left join disease d
  on j.dis_id = d.dis_id

如果您需要帮助学习加入语法,请查看这个有用的visual explanation of joins

请注意,我在INNER JOINpatient之间使用了history,并加入了patient中两个键上的表格。这种类型的连接将返回两个表中的所有匹配记录。

我在其他两个表上使用LEFT JOIN,即使其他两个表中没有匹配的记录,也会返回所有患者/历史数据。根据您的需要,您可以在这些表格上使用INNER JOIN

相关问题