【问题标题】:SQL Joining tables and displaying dataSQL 连接表和显示数据
【发布时间】:2013-03-17 05:31:27
【问题描述】:

我有 3 张桌子与我遇到问题的医院有关。

入院包含患者编号、入院日期、出院日期和病房。
医生医生编号、姓氏、名字和病房。
病房 wardid,姓名和顾问(医生)

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

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

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

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

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

【问题讨论】:

  • 一篇文章的问题太多了。将其分解为更小的问题。

标签: sql join


【解决方案1】:

患者不使用的医生

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

【讨论】:

    猜你喜欢
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    相关资源
    最近更新 更多