SQL query that meets a parameter that is a set

Asked

Viewed 1,443 times

2

I have the following situation: in a MEDICAL CONSULTATION I may have one or more SYMPTOMS. Then I have a CONSULTA_SINTOMA table, making the link between CONSULTATION and SYMPTOM. A disease can be identified by one or more symptoms, so I have a DON_SINTOMA table making the link between DISEASE and SYMPTOM.

My problem: I can get in the QUERY table all the ids of symptoms presented in a query. But how can we make sure that we know all the possible diseases? For example: if the symptoms presented in a consultation are: 1, 2 and 3. How to know which diseases are identified by these symptoms?

  • You want the diseases with the 3 symptoms, or at least one of the 3?

  • With the 3 symptoms.

  • @Allexandrejr take a look at my amendment

4 answers

2

I don’t know how your columns are, but I tried to deduce that the keys are ID_TABELA.

The disease has at least one symptom:

  SELECT CO.ID_CONSULTA, SI.ID_SINTOMA, DO.ID_DOENCA FROM CONSULTA CO
    INNER JOIN CONSULTA_SINTOMA CS ON CO.ID_CONSULTA = CS.ID_CONSULTA
    INNER JOIN SINTOMA SI ON CS.ID_SINTOMA = SI.ID_SINTOMA
    INNER JOIN DOENCA_SINTOMA DS ON SI.ID_SINTOMA = DS.ID_SINTOMA
    INNER JOIN DOENCA DO ON DS.ID_DOENCA = DO.ID_DOENCA

For each disease will repeat the symptom and query, you just need to exchange the fields you want to bring in SELECT.


The disease have all symptoms: (Using the operator ALL)

  SELECT * FROM DOENCA DO
    INNER JOIN DOENCA_SINTOMA DS ON DO.ID_DOENCA = DS.ID_DOENCA
    WHERE DS.ID_SINTOMA = ALL (SELECT CS.ID_SINTOMA 
                               FROM CONSULTA_SINTOMA CS
                               WHERE CS.ID_CONSULTA = x)

x will be your consultation code

  • Query made in Mysql 5.5.32
  • Maicon, thus is returning the diseases that are identified by at least 1 of the symptoms. I need those that are identified by all symptoms.

  • @Allexandrejr I updated the code according to your condition.

  • Opa @Maicon Carraro, even in this second case, returns me appointments that has at least 1 of the symptoms.

  • @Allexandrejr returns consultations or diseases with 1 of the symptoms?

  • Sorry, @Maicon Carrado. Returns the diseases. But I already managed to solve by following the example of bfavaretto. Thanks for the help.

1


There are a few ways to do this. One I quite like uses a GROUP BY and prior knowledge of how many symptoms you need to marry.

An example, assuming the actual structure of your database:

SELECT
    Doenca.*

FROM CONSULTA_SINTOMA cs

    INNER JOIN DOENCA_SINTOMA ds
    ON ds.sintoma_id = cs.sintoma_id
    INNER JOIN DOENCA Doenca
    ON Doenca.id = ds.doenca_id

WHERE cs.consulta_id = 1  -- a consulta
AND cs.sintoma_id IN (1,2,3) -- os sintomas

GROUP BY Doenca.id
HAVING COUNT(*) = 3 -- porque são 3 sintomas

A shorter variation, which does not require passing the symptoms explicitly, but still requires knowing how many are:

SELECT
    Doenca.*
FROM DOENCA_SINTOMA ds
    INNER JOIN DOENCA Doenca
    ON Doenca.id = ds.doenca_id
WHERE ds.sintoma_id IN (
   SELECT sintoma_id FROM CONSULTA_SINTOMA
   WHERE consulta_id = 1 -- a consulta
) -- os sintomas

GROUP BY Doenca.id
HAVING COUNT(*) = 3 -- porque são 3 sintomas
  • @bfavarreto I had tried the IN, but, it returns diseases that has at least 1 of the symptoms. Qdo accurate of the diseases that have all the symptoms.

  • Try this entire query. The grouping + HAVING COUNT(*) = 3 ensures that only those with the 3 symptoms come.

  • It’s true @bfavaretto didn’t pay attention to having. I think so. I’ll try to see. ;)

  • It worked @bfavaretto. I only switched the symptoms with the respective select.

  • Good, @Allexandrejr! After rereading my code, I saw that you can simplify the query, so you only need to pass the query id. I will update the reply soon.

0

I haven’t tested.
Quick response so you can test:

SELECT SI.doencaId FROM sintomas SI    
WHERE SI.sintomasId IN (
  SELECT CO_SI.sintomasID FROM consultas_sintomas CO_SI 
  WHERE CO_SI.consultaId = id
)  

id - id of the query for which you want to know the possible diseases

0

First you must create a table of diseases and another of symptoms and then one of relationship between diseases and symptoms. For appointments you should do something pareciso, one of consultations and the other of relationship symptoms-consultations.

Hence it is easy to find out which diseases present the n symptoms.

Note: I hope this is a "rhetorical" question and not in order to create a diagnostic system.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.