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

Display User That Is Does Not Exist in Other Table

我有一个查询,我应该显示医生可以接诊的患者数量。他一次最多只能接待 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)

以下是 table 创建的内容,旨在帮助大家更好地理解 table 与

之间的关系
    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));

两个 table 查询 return 完全相同的东西。请看图 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);

你可以用这个

select PHYSICIAN.PHYSICIAN_ID, PHYSICIAN.firstname_physician, PHYSICIAN.lastname_physician, phone.phone_number, 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) = 0;