簡體   English   中英

SQL連接表並顯示數據

[英]SQL Joining tables and displaying data

我有3張桌子與我遇到麻煩的醫院有關。

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

入院包含患者編號,入院日期,出院日期和病房。
醫生 Doctorid,姓,名和病房。
病房的病房 ,名稱和顧問(醫生)

我已經能夠完成很多本教程要問的問題,但是我找不到有關如何執行以下操作的任何指導或答案:

我希望找到不被患者使用的醫生的姓氏,並且還要顯示患者所使用的醫生的姓氏。 我猜想加入Doctor and Ward表格,並以某種方式入場表明530位醫生沒有被任何患者使用。

然后,我應該有一個AND,以在病房表中顯示與顧問對應的醫生的姓氏。

要查找尚未治療任何患者的醫生的詳細信息,我將與Ward一起加入Doctor,並在顧問列中顯示未處理的醫生的詳細信息。 我了解這個理論,只是不確定如何在SQL中正確地解決它。

我歡迎任何討論或對答案的幫助,因為我真的很想了解這一步。

病人不使用的醫生

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM