查询无法使用DATE_FORMAT

时间:2014-03-01 16:55:24

标签: mysql sql

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
where d.status = 1 and  DATE_FORMAT(ap.appDate, '%Y-%m') = '2013-10'

我的下面的查询在我做错了之后给我错误请帮帮我?

where d.status = 1 and  DATE_FORMAT(ap.appDate, '%Y-%m') = '2013-10'

错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where d.status = 1 and ds.spID and DATE_FORMAT(ap.appDate, '%Y-%m') = '2013-10' at line 9

2 个答案:

答案 0 :(得分:1)

where子句位置错误。它位于group by

之前
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 
where d.status = 1 and  DATE_FORMAT(ap.appDate, '%Y-%m') = '2013-10'
GROUP BY d.doctorID;

答案 1 :(得分:0)

您需要交换这两行

where d.status = 1 and  DATE_FORMAT(ap.appDate, '%Y-%m') = '2013-10'
GROUP BY d.doctorID