Select 来自 Table 的多个不同值并评估它们以匹配 table

Select Multiple Different Values from Table and Evaluate them for a match in table

我在尝试弄清楚如何 select 具有多个值的多个记录时遇到了一些麻烦。让我试着分解一下。

我有两个table。

Table 1: 疾病

Table 2:症状

我想做的是当用户输入症状时,它会检查疾病 table 并找到与之相关的匹配症状,然后它会 return 具有的疾病给出的症状。

这是我目前的 SQL 查询:

select a.diseases_id, a.disease_name
from diseases a, symtoms b 
where a.diseases_id = b.diseases_id
and b.symtom_description like '%Cough%' AND b.symtom_description like '%Sore throat%';

现在的问题是这个 SQL 查询 return 什么都没有,但是如果我删除 AND 语句它 return 包含 "Cough" 中的所有疾病他们的症状,但这不是我想要的。我应该能够 select 一堆症状,如果一种疾病具有所有给出的症状并且它们匹配,它将 return 疾病。对于我发布的查询,它应该 return "Gastroesophageal Reflux" 因为是仅包含咳嗽 喉咙痛的疾病。

任何人都可以告诉我如何进行这项工作吗?

谢谢!

没有包含 "Cough" AND "Sore throat" 的行,所以你当然会得到一个空结果。

我会尝试这样的事情(未经测试):

select a.diseases_id, a.disease_name, b.symtom_description
from diseases a, symtoms b 
where a.diseases_id = b.diseases_id
and (b.symtom_description like '%Cough%' OR b.symtom_description like '%Sore throat%')
group by a.diseases_id
having count(a.diseases_id)=2

可以使用以下符合 ANSI 标准的连接语句编写上述查询

SELECT a.diseases_id, a.disease_name, b.symptom_description
FROM diseases AS a JOIN symptoms AS b 
     ON a.diseases_id = b.diseases_id
WHERE b.symptom_description like '%Cough%' OR b.sypmtom_description like '%Sore throat%'
 GROUP BY a.diseases_id
    HAVING COUNT(a.diseases_id) = 2