我的mysql查询
SELECT count(ICD10) as columnDis, ICD10
FROM(
SELECT tbl_disease.ICD10, tbl_symptom_disease.symptomCode
FROM `tbl_disease`
JOIN tbl_symptom_disease
ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
WHERE tbl_symptom_disease.symptomCode='P18'
OR tbl_symptom_disease.symptomCode='P19'
) AS tbl_ICD10_symCode
GROUP BY ICD10
HAVING columnDis=?????
因此,此查询将返回所有具有重复值等于?????的ICD10条目。
在这个例子中,重复的最大数量是2,但我想将它用于不同的sympomCode(s),在这种情况下,可能有3个重复的条目出现5次,5个其他的出现3次,1个出现2次,依此类推。所以我需要的只是出现MOST次数的重复条目。类似的东西:
HAVING columnDis=(MAX(columnDis))
但那当然不起作用。有人可以帮帮我吗?
答案 0 :(得分:1)
使用子查询获取最大计数,然后将其与主要子查询
连接SELECT ICD10
FROM
(
SELECT MAX(disease_count) AS max_disease_count
FROM
(
SELECT tbl_disease.ICD10, COUNT(tbl_disease.ICD10) AS disease_count
FROM `tbl_disease`
JOIN tbl_symptom_disease
ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
WHERE tbl_symptom_disease.symptomCode='P18'
OR tbl_symptom_disease.symptomCode='P19'
GROUP BY ICD10
) sub0
) sub1
INNER JOIN
(
SELECT tbl_disease.ICD10, COUNT(tbl_disease.ICD10) AS disease_count
FROM `tbl_disease`
JOIN tbl_symptom_disease
ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
WHERE tbl_symptom_disease.symptomCode='P18'
OR tbl_symptom_disease.symptomCode='P19'
GROUP BY ICD10
) sub2
ON sub1.max_disease_count = disease_count