我有三张桌子
我希望按症状数量匹配排序疾病,症状由最终用户输入并存储在变量$ searchSymptoms中。
我尝试跟踪查询并做了一些改动,得到了一个非常奇怪的结果。
SELECT DISTINCT ailments.ailmentName,COUNT(symptoms.symptomID) AS count
FROM ailments LEFT JOIN jnctn_ailments_symptoms ON ailments.ailmentID = jnctn_ailments_symptoms.FK_ailmentID
LEFT JOIN symptoms ON ailments.ailmentID = jnctn_ailments_symptoms.FK_ailmentID
GROUP BY ailments.ailmentName ORDER BY count DESC
结果:
如果有任何答案,我将有责任。
问候
答案 0 :(得分:0)
查询可以写成如下:
SELECT ailmentID, ailmentName, ailmentSymptomCount
FROM ailments a
LEFT JOIN(
SELECT FK_ailment_id, COUNT(FK_symptom_id) AS ailmentSymptomCount
FROM jnctn_ailments_symptoms GROUP BY FK_ailment_id
) temp
ON a.ailmentID = temp.FK_ailment_id