在其他表中不存在的显示用户

时间:2018-12-14 04:01:58

标签: sql oracle having

我有一个查询,应该显示医生可以接受的患者人数。他一次最多只能有5位患者。我使用以下查询进行此查询:

select PHYSICIAN.PHYSICIAN_ID,PHYSICIAN.firstname_physician,PHYSICIAN.lastname_physician, phone.phone_number, 5-count(patient.patient_id) as "Numbers of new patients he/she can take"
from patient, physician, physician_phone, phone
where physician.physician_id = patient.physician_id and PHYSICIAN_PHONE.PHYSICIAN_ID = PHYSICIAN.PHYSICIAN_ID and phone.PHONE_ID = physician_phone.PHONE_ID
group by PHYSICIAN.PHYSICIAN_ID, PHYSICIAN.firstname_physician, PHYSICIAN.lastname_physician, physician_phone.phone_id, phone.phone_number
having count(patient.patient_id)<5;

但是,这仅显示具有患者的医师,而不显示连接了0位患者的医师。

我试图展示也有0位患者的医生是:

    select PHYSICIAN.PHYSICIAN_ID,PHYSICIAN.firstname_physician,PHYSICIAN.lastname_physician, phone.phone_number, 5-count(patient.patient_id) as "Numbers of new patients he/she can take"
from patient, physician, physician_phone, phone
where physician.physician_id = patient.physician_id and PHYSICIAN_PHONE.PHYSICIAN_ID = PHYSICIAN.PHYSICIAN_ID and phone.PHONE_ID = physician_phone.PHONE_ID
group by PHYSICIAN.PHYSICIAN_ID, PHYSICIAN.firstname_physician, PHYSICIAN.lastname_physician, physician_phone.phone_id, phone.phone_number
having count(patient.physician_id)<5 OR NOT EXISTS ( Select patient.physician_id from patient  Where patient.physician_id != physician.physician_Id group by patient.physician_id)

下面是表格的创建,以帮助任何人更好地了解表格之间的关系

    Create Table Physician (
    Physician_ID        integer     not null,
    Firstname_physician Char(30Char)     not null,
    lastname_physician  Char(30Char)     not null,

    Constraint Physician       Primary Key (Physician_ID));
Create Table Patient (
Patient_ID                  integer           not null,
Patient_FirstName           Char(20Char)        not null,
Patient_LastName            Char(20Char)        not null,
Patient_MI                  Char(1Char)         not null,
Patient_Gender              Char(15Char)        not null,
Staff_id                    integer             not null,
Physician_ID                integer             not null,

Constraint Patient_pk             Primary Key     (Patient_ID),
Constraint HomeCareStaff_fk       Foreign Key     (Staff_ID)    References HomeCareStaff(Staff_id),
Constraint Physician_ID_fk10      Foreign Key      (Physician_ID)   References Physician(Physician_ID));

两个表查询都返回相同的内容。请查看图片results of query

希望这很有意义这也是插入内容

--populating Physician table--
insert into Physician values (100, 'Sasia', 'Applebottom');
insert into Physician values (101, 'Mac', 'Cheese');
insert into Physician values (102, 'Mick', 'Donalds');
insert into Physician values (103, 'Saint', 'West');
insert into Physician values (104, 'Chicago', 'West');
insert into Physician values (105, 'Mason', 'Disic');

---Populate Patient Table
insert into Patient values (150, 'Hayley', 'Beachump', 'F', 'Female', 50, 100);
insert into Patient values (151, 'Jacob', 'Stutzmen', 'K', 'Male', 51, 100);
insert into Patient values (152, 'Christina', 'Smush', 'P', 'Female', 52, 100);
insert into Patient values (153, 'Doris', 'Dorphish', 'D', 'Female', 53,100);
insert into Patient values (154, 'Adam', 'Wang', 'M', 'Male', 54, 100);
insert into Patient values (155, 'Levina', 'Reinhart', 'U', 'Female', 55, 101);
insert into Patient values (156, 'Harper', 'Mosbey', 'M', 'Male', 56, 102);

1 个答案:

答案 0 :(得分:1)

您可以使用此

HttpClient