如何统计没有患者预约的医生?

时间:2016-05-11 19:18:39

标签: php mysql count

结果如下:

Doctors_have_no_patients
Doctor2
Doctor3
Doctor5

表格

Doctor table called `Doctors`
**DoctorID**
Doctor1
Doctor2
Doctor3
Doctor4
Doctor5


Booking table called `Bookings`

PatientID  DoctorID  Date
Patient1   Doctor1   etc.
Patient2   Doctor4   etc.
Patient3   Doctor1   etc.

我应该使用distinct还是其他什么?像:

select Bookings.DoctorID as Doctors_have_no_patients count(distinct(Bookings.PatientID))...

2 个答案:

答案 0 :(得分:2)

你可以不使用(在这种情况下没有区别)

无病人的医生数量

select count(*) from Doctors
where DoctorId not in (select doctorID from bookings);

医生的身份

select DoctorID from Doctors
where DoctorId not in ( select doctorID from bookings);

答案 1 :(得分:0)

使用以下查询

   SELECT * from `Doctors`
    WHERE `DoctorID` NOT IN (SELECT DISTINCT(`doctorID`) FROM `bookings`);