我有三张桌子Bill,Patient和Doctor。
每个病人的账单细节与助理医生一起在账单表中显示。然而,在病人表中,每个病人的一般细节都与SuperVisor医生绘制了图表。“医生”表包含所有辅助人员和主管提供者的详细信息及其附加信息。
我想显示医生的名字,在输出的Doctor_ID从病人表,而不是从医生表。
例如:如果病人有XYZ SuperVisor医生,并且在记帐时,他的详细信息被ABC医生所珍视,那么我想向XYZ医生展示,而不是ABC。
请帮帮忙。
Patient
Patient_ID Doctor_ID
0004G 0000A
Doctor
Doctor_ID Name
0000M XYZ
0000A ABC
Bill
Patient_ID Doctor_ID
0004G 0000M目前,我正在使用下面的Joins:
FROM BILL b
INNER JOIN Patient pt ON b.Patient_ID = pt.Patient_ID
INNER JOIN Doctor ep ON b.Doctor_ID = ep.Doctor_ID我不想改变这个。那如何才能做到这一点呢?
发布于 2019-08-01 10:53:09
你可以试试这个。
select pat.patientid, doc.docname from patient as pat
inner join bill_detail as bill on pat.patientid=bill.patientid
inner join doctor as doc on bill.doctorid=doc.doctorid与其从病人桌上获取医生记录,不如从账单表中获取。这份法案和病人表在PatientId上是相互关联的。另一方面,比尔和博士桌是相关的DoctorId。
或
如果您想向直接医生展示,那么简单的连接就足够了。
select pat.patientid, doc.docname from patient as pat
inner join doctor as doc on pat.doctorid=doc.doctorid发布于 2019-08-01 11:01:40
试试这个-
SELECT B.*,
D.Name [Supervisor Doctor Name]
FROM Bill B
INNER JOIN Patient P ON B.Patient_ID = P.Patient_ID
INNER JOIN Doctor D ON P.Doctor_ID = D.Doctor_ID如果你也需要助理医生的名字,请使用下面的代码-
SELECT B.*,
D.Name [Supervisor Doctor Name],
DD.Name [Assistant Doctor Name]
FROM Bill B
INNER JOIN Patient P ON B.Patient_ID = P.Patient_ID
INNER JOIN Doctor D ON P.Doctor_ID = D.Doctor_ID
INNER JOIN Doctor DD ON B.Doctor_ID = DD.Doctor_ID发布于 2019-08-01 11:09:36
你的问题有点含糊,但我假设你有一个病人,一个医生和一个监督医生。为此,您需要在医生表或账单表中添加supervisor_id字段,具体取决于主管是否总是相同的。您可以尝试以下操作:alter table Doctor add column Supervisor_id integer --这将在您的医生表中添加一个列。在此之后,您可以做一个选择,以获得您的主管的名字,为每一个帐单。select bill.*, supervisor.name from bill inner join doctor assist on bill.doctor_id = assist.doctor_id inner join doctor supervisor on assist.supervisor_id = supervisor.doctor_id
https://stackoverflow.com/questions/57307288
复制相似问题