所以...这有点复杂...特别注意我在表格中包含的示例数据。
http://sqlfiddle.com/#!2/04eac4/15/0
http://sqlfiddle.com/#!2/04eac4/17/0
我不知道应该在哪里进行分裂......?
首先我们需要知道每个城市有多少家诊所:
select city, count(cid) as c_count from clinic group by city
然后我们需要知道每个病人在哪里接受了除流感以外的任何治疗......
select patient.pid, patient.pname, clinic.city,
count(distinct clinic.cid) as p_c_count
from patient
join patient_doctor
on patient.pid = patient_doctor.pid
join doctor
on patient_doctor.did = doctor.did
join clinic
on doctor.cid = clinic.cid
where patient_doctor.disease not in ('Flu')
group by patient.pid, patient.pname, clinic.city
这将所有表连接在一起,以便我们可以链接PID
和CID
。然后,我们只需要关于不包括流感诊断的患者的信息。之后,我们按患者信息和诊所城市分组,这样我们就可以统计每个城市患者就诊的诊所数量。请记住,患者 A 可以去第一个城市的诊所 1 接受流感治疗,即使他在不同城市的不同诊所接受流感治疗,您提出问题的方式也允许该人展示第一个城市/诊所的结果....这有什么意义吗?
然后我们需要加入这两个结果。如果每个城市的诊所数量等于每个城市患者就诊的诊所数量,而没有被诊断出患有流感,我们得到:
select p_info.pid, c_ct_info.city
from (select city, count(cid) as c_count from clinic group by city) as c_ct_info
join (
select patient.pid, patient.pname, clinic.city,
count(distinct clinic.cid) as p_c_count
from patient
join patient_doctor
on patient.pid = patient_doctor.pid
join doctor
on patient_doctor.did = doctor.did
join clinic
on doctor.cid = clinic.cid
where patient_doctor.disease not in ('Flu')
group by patient.pid, patient.pname, clinic.city ) as p_info
on c_ct_info.city = p_info.city
where c_count = p_c_count;
现在。这不包括那些刚去过一家诊所但没有被诊断出患有流感但没有去该市任何其他诊所的人。
以防万一 sqlfiddle 有一天死了……这是我使用的表和数据:
Create table Clinic (
CID int not null primary key,
Cname varchar(100),
Mname varchar(100),
Type tinyint,
City varchar(100)
);
insert into Clinic values (1,'Clinic 1','Mname 1', 1, 'City 1');
insert into Clinic values (2,'Clinic 2','Mname 2', 1, 'City 1');
insert into Clinic values (3,'Clinic 3','Mname 3', 1, 'City 2');
Create table Doctor (
DID int not null primary key,
Dname varchar(100),
Specialty varchar(100),
CID int,
foreign key (CID) references Clinic (CID)
);
insert into Doctor values (1, 'House', 'Internal Medicine', 1);
insert into Doctor values (2, 'Dr Who', 'General Practice', 1);
insert into Doctor values (3, 'Dr Dave', 'General Practice', 1);
insert into Doctor values (4, 'Dr Four', 'General Practice', 2);
insert into Doctor values (5, 'Dr Five', 'General Practice', 3);
insert into Doctor values (6, 'Dr Six', 'General Practice', 3);
create Table Patient (
PID int not null primary key,
PName varchar(100)
);
insert into Patient values (1, 'P. One');
insert into Patient values (2, 'P. Two');
insert into Patient values (3, 'P. Three');
insert into Patient values (4, 'P. Four');
insert into Patient values (5, 'P. Five');
Create table Patient_Doctor (
PDID int not null auto_increment primary key,
PID int not null,
DID int,
Disease varchar(100),
Severity tinyint,
foreign key (PID) references Patient (PID),
foreign key (DID) references Doctor (DID)
);
insert into Patient_Doctor values (null, 1, 1, 'Flu', 1);
insert into Patient_Doctor values (null, 1, 4, 'Flu', 1);
insert into Patient_Doctor values (null, 1, 5, 'Flu', 1);
-- shouldn't be in our results because they were diagnosed with the flu in each city
insert into Patient_Doctor values (null, 2, 2, 'Other', 1);
insert into Patient_Doctor values (null, 2, 4, 'Other', 1);
insert into Patient_Doctor values (null, 2, 5, 'Other', 1);
-- should be in our results because they attended every clinic in every city and
-- did not get diagnosed with the flu at any location
insert into Patient_Doctor values (null, 3, 1, 'Other', 1);
insert into Patient_Doctor values (null, 3, 4, 'Other', 1);
insert into Patient_Doctor values (null, 3, 6, 'Flu', 1);
-- should show up in our results for City 1 because they attended all the clinics in that
-- city and were not diagnosed with the flu. this person should NOT show up in our results
-- for city 2 because they were diagnosed with the flu at a clinic there.
insert into Patient_Doctor values (null, 4, 3, 'Other', 1);
-- should NOT show up in any results. although they weren't diagnosed with the flu, they
-- did not attend each clinic in that city.
insert into Patient_Doctor values (null, 5, 2, 'Flu', 1);
-- should NOT show up in results... meets none of the criteria
如果我们要添加这些数据:
insert into Patient values (6, 'P. Six');
insert into Patient_Doctor values (null, 6, 5, 'Other', 1);
我们应该期望在我们的结果中看到那个人,因为 2 号城市只有一家诊所,他们在那里没有被诊断出患有流感……
我改变的事情:
Patient_Doctor
:PID、DID、疾病、严重性
Patient
:PID,P名称
Clinic
Doctor
现在加入了而CID
不是CName
- 我强烈建议您创建另一个表,
Disease
这样您就不会一遍又一遍地重复相同的信息。该表将包括字段Disease_ID
和Disease_Description
。然后,该疾病将通过Disease_ID
.