Felipe
Felipe

Reputation: 303

How to make an SQL statement that brings only the record that matches the two parameters provided?

Let's suppose I have a table called Diseases, with one column:

 "disease_name" (primary Key)

And on the other hand I have a table called Symptoms, with three columns:

 "id" (primary key auto generated number)
 "symptom_name"
 "disease_name_fk" (foreign key from table Diseases "disease_name")

Given the schema above, I want to retrieve the disease based on the symptoms.

For example I have the symptoms "fever" and " body pain", I want to retrieve only the diseases that matches those two symptoms from table symptons. If the disease has only one matche then it is worthless, it has to match the two parameters given, returning the diseases names corresponding to those symptoms.

How can I do that?

Upvotes: 0

Views: 19

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269853

You can use group by and having:

select disease_name_fk
from symptoms
where symptom_name in ('fever', 'body pain')
group by disease_name_fk
having count(*) = 2;  -- number of symptoms in list

Upvotes: 1

Related Questions