to be more specific, I have a table with a list of symptoms and a table with a list of diseases. I have also created a table to link a specific symptom to a specific disease. What I’m looking to do is returning all the diseases that have ALL the selected symptoms from the symptoms’table.
Symptom
id | symptom |
---|---|
1 | Symp A |
2 | Symp B |
3 | Symp C |
4 | Symp D |
5 | Symp E |
Disease
id | disease |
---|---|
1 | Dss A |
2 | Dss B |
3 | Dss C |
4 | Dss D |
Symptom_Disease
id | symp_id | dss_id |
---|---|---|
1 | 1 | 2 |
2 | 1 | 4 |
3 | 2 | 2 |
4 | 2 | 1 |
5 | 3 | 1 |
6 | 4 | 1 |
So, say I select the symptoms A and B (whose ids are 1 and 2), the only disease with both those symptoms is the disease B (id 2). Is there a way to write a single select (with its joins / nested SELECTS / whatsoever) to achieve this result?
I’ve tried something like:
SELECT *
FROM `disease`
LEFT JOIN symptom_disease ON symptom_disease.id_disease = disease.id
LEFT JOIN symptom ON symptom.id = symptom_disease.id_symptom
WHERE symptom_disease.id_symptom = 1
AND symptom_disease.id_symptom = 2;
but of course it doesn’t work.
Thank you in advance.
3
Answers
you can use a qeurie like this:
NOTE: You only must change the last 2 lines with this symp ids and the number of symps
sample
This answer avoids using ID’s as criteria:
Diseases that include both symptoms 1 and 2…
Diseases that have exactly symptoms 1 and 2 (and no other additional symptoms)