2016-05-11 41 views
0

結果應該是這樣的:如何計算沒有患者預約的醫生?

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))...

+3

你需要養成[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)這樣的習慣來幫助你解決你的問題的問題。您將獲得積分,其他人將被鼓勵幫助您。 –

+0

您需要在您的'預訂'選擇中使用'distinct',就像您寫的@Tews –

回答

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); 
0

使用以下查詢

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

OP爲什麼要「使用以下內容」?一個好的答案***將總是解釋所做的事情以及爲什麼這樣做,不僅是爲了OP,還是爲了將來訪問SO。 –

相關問題