2014-03-01 88 views
1

我的下面的查詢不工作,因爲我接受基本上我願意爲每位醫生提供所有記錄,用逗號分隔的他們的專業,也取得每個醫生約會與特定月份,但查詢重複逗號分隔的專業。加入查詢group_concat,重複值

select d.doctorFName,d.doctorLName ,count(ap.appointmentID) totalap, 
GROUP_CONCAT(s.speciality) specialities 
FROM tbl_doctors d 
INNER JOIN tbl_doctor_speciality ds ON (d.doctorID = ds.doctorID) 
INNER JOIN tbl_speciality s ON (s.spID = ds.spID) 
Inner join tbl_appointment ap on (ap.doctorID = d.doctorID) 
Inner join tbl_patients p on p.patientID = ap.patientID 
GROUP BY d.doctorID 

我這工作好每名醫生和他們的專業以逗號分隔

select d.doctorFName,d.doctorLName , 
GROUP_CONCAT(s.speciality) specialities 
FROM tbl_doctors d 
INNER JOIN tbl_doctor_speciality ds ON (d.doctorID = ds.doctorID) 
INNER JOIN tbl_speciality s ON (s.spID = ds.spID) 
GROUP BY d.doctorID 

但是當我添加加入任命其重複的特色,下面是我的數據庫,我希望你能理解數據通過query.where我做錯了?以及如何我可以使用

DATE_FORMAT(ap.appDate, '%Y-%m') = '2013-10' 

與上面的查詢監守ITRS得到錯誤

回答

2

嘗試與GROUP_CONCAT DISTINCT,內部連接的一對多將產生笛卡爾乘積,所以你將有重複值

select d.doctorFName,d.doctorLName ,count(ap.appointmentID) totalap, 
GROUP_CONCAT(DISTINCT s.speciality) specialities 
FROM tbl_doctors d 
INNER JOIN tbl_doctor_speciality ds ON (d.doctorID = ds.doctorID) 
INNER JOIN tbl_speciality s ON (s.spID = ds.spID) 
Inner join tbl_appointment ap on (ap.doctorID = d.doctorID) 
Inner join tbl_patients p on p.patientID = ap.patientID 
GROUP BY d.doctorID 
+0

@M哈立德Junaid,我怎麼可以使用DATE_FORMAT(ap.appDate,'%Y-%m')='2013-10'與哪裏cluase? –

+0

'ap.appDate> ='2013-10-01'AND ap.appDate <'2013-10-01'+ INTERVAL 1 MONTH'瞭解D​​ATETIME索引範圍掃描。但這是一個不同的問題。 –