0

我有 3 张桌子与我遇到麻烦的医院有关。

http://postimage.org/image/hi2c4yrrf/

入院包含患者ID、入院日期、出院日期和病房。
医生,姓,名和病房。
Ward wardid,姓名和顾问(博士)

我已经能够做很多教程要求我做的事情,但是我找不到任何关于如何执行以下操作的指导或答案:

我希望找到患者未使用的医生的姓氏,并显示患者使用的姓氏。我猜想加入医生和病房表,并以某种方式入院以表明医生 530 没有被任何患者使用。

然后我应该有一个 AND 来显示病房表中与顾问对应的医生的姓氏。

要查找未治疗任何患者的医生的详细信息,我将加入 Doctor with Ward 并显示不在顾问列中的医生的详细信息。我理解这个理论,我只是不确定如何在 SQL 中正确地解决它。

我欢迎任何讨论或帮助,因为我真的很想了解这一步。

4

1 回答 1

0

患者不使用的医生

select distinct d.number, d.surname
from doctor d
left join ward w on w.consultant = d.number
left join admission ad on ad.ward = w.code
left join patient p on p.code = ad.patient
where p.code is null

患者使用的医生

select distinct d.number, d.surname
from doctor d
join ward w on w.consultant = d.number
join admission ad on ad.ward = w.code
join patient p on p.code = ad.patient

架构(用于测试):

select * into patient
from (
  select 'A102' [code], 'Harris' [surname], 'Lucy' [lastname] union all 
  select 'B372', 'Rossini', 'Peter' union all
  select 'B534', 'Johnson', 'Nadia' union all
  select 'B444', 'Johnson', 'Juigi' union all
  select 'S555', 'Rose', 'Jean') as p

select * into admission
from (
  select 'A102' [patient], null [admitted], NULL [discharged], 'A' [ward] union all
  select 'A102', null, NULL, 'A' union all
  select 'S555', null, NULL, 'B' union all
  select 'B444', null, NULL, 'B' union all
  select 'S555', null, NULL, 'A') as ad

select * into doctor
from (
  select 203 [number], 'Black' [surname], 'Peter' [firstname], 'A' [ward] union all
  select 574, 'Blis', 'Mavis', 'B' union all
  select 461, 'Boyne', 'Steve', 'B' union all
  select 530, 'Clark', 'Nicola', 'C' union all
  select 405, 'Mizzi', 'Nicola', 'A' union all
  select 501, 'Mount', 'Mavis', 'A') as d

select * into ward
from (
  select 'A' [code], 'Surgical' [name], 203 [consultant] union all
  select 'B', 'Paediatric', 574 union all
  select 'C', 'Medical', 530) as w
于 2013-03-17T06:04:34.823 回答