Hospital_Visit
hid pid HospitalName DoctorId
41 1 abc 1
42 2 xyx 2
Patient_Master
pid PatientName
1 jill
2 rosy
Doctor_Master
DoctorID DoctorName
1 John
2 Jack
Hospital_Study
sid hid exam status
1 41 jjj sfvn
2 41 fks jdjd
select Hospital_Visit.Pid,PatientName,DoctorName from Patient_Master
inner join Hospital_Visit on Hospital_Visit.pid=Patient_Master.pid
inner join Doctor_Master on Doctor_Master.DoctorID= Hospital_Visit.DoctorID
inner join Hospital_Study on Hospital_Study.hid=Hospital_Visit.hid
Pid PatientName DoctorName exam status
1 Jill John jjj sfvn
2 rosy John fks jdjd
**
//Correct output i want
Pid PatientName DoctorName exam status
1 Jill John jjj sfvn
2 rosy Jack fks jdjd
**
inner join
由于隐藏在 Hospital_Visit 和 Hospital_Study 上,我得到了错误的结果重复医生姓名
我该如何解决这个问题
(DTU 编辑 - 可用形式的当前样本数据):
create table Hospital_Visit(hid int,pid int,HospitalName char(3), DoctorId int)
insert into Hospital_Visit(hid, pid, HospitalName, DoctorId) values
(41, 1, 'abc', 1),
(42, 2, 'xyx', 2)
create table Patient_Master(pid int, PatientName char(4))
insert into Patient_Master(pid, PatientName) values
(1, 'jill'),
(2, 'rosy')
create table Doctor_Master(DoctorID int, DoctorName char(4))
insert into Doctor_Master(DoctorID, DoctorName) values
(1, 'John'),
(2, 'Jack')
create table Hospital_Study(sid int, hid int, exam char(3), status char(4))
insert into Hospital_Study(sid, hid, exam, status) values
(1, 41, 'jjj' ,'sfvn'),
(2, 41, 'fks' ,'jdjd')