这是我的查询
SELECT
tblDiseases.disease
FROM
tblRel
LEFT JOIN
tblDiseases ON tblRel.diseaseID = tblDiseases.diseaseID
WHERE
tblRel.symptomID = '1' AND tblRel.symptomID = '2' AND tblRel.symptomID = '3'
这是我的桌子
#tblDiseases - holds all disease names
######################################
diseaseID | disease
-----------------------
1 Tifoyd
2 Jondis
3 Malarya
4 Pneomonia
5 Dengu
#tblSymptoms - holds all symptoms
#################################
symptomID | symptom
-------------------------
1 Headache
2 Temparature
3 Less Pain
4 Sever Pain
5 Mussle Pain
#tblRel - holds relation between diseases and symptoms
######################################################
relID | dieaseID | symptomID
-----------------------------
1 1 1
2 1 2
3 3 1
4 3 2
5 3 3
我选择了症状为“头痛”,“体温”和“疼痛减轻”的疾病,因此应该给“马拉拉”,但什么也不能给。
答案 0 :(得分:0)
单行中的单列不能具有三个不同的值。您想要的是聚合,以比较不同行中的值:
SELECT d.disease
FROM tblRel r JOIN
tblDiseases d
ON r.diseaseID = d.diseaseID
WHERE r.symptomID IN (1, 2, 3)
GROUP BY d.disease
HAVING COUNT(*) = 3; -- has all three symptoms
请注意,LEFT JOIN
不是必需的,因为您需要使用匹配项来命名疾病。 (大概,表之间的疾病编号匹配,就像在格式良好的数据库中一样。)