Question

my below query is not working as i accept basically i am willing to fetch all the record for each doctor with their specialties by comma separated and also fetch each doctor appointments with specific month but the query repeating comma separated specialties.

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

my this is working good with each doctor and their specialties by comma separated

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 

but when i add join for appointment its repeating specialties,below is my database,i hope you will understand data by query.where i doing wrong? and also how i can use

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

with above query becuase itrs getting error

Was it helpful?

Solution

Try with DISTINCT in group_concat, inner join for one to many will generate the Cartesian product so you will have the repeated values

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top