dimanche 19 juin 2016

Advanced query with joined tables and multiple values from GET parameter

First of all I'm going to explain what I want to happen in the end so if you can't help me you don't waste your time reading all of this :)

I'm creating an API in which it outputs possible illnesses that a user could have, based on the symptoms they submitted.

I want the user to input something, this goes into the url as "symptom=... like: .../nameofmyapi/data?symptoms=headache+dizzyness. I retrieve the data from the GET parameter, but I have absolutely no idea on how to output the name of the illness.

My database looks like this:

  • illness

    • illnessId (int(11), AUTO_INCREMENT)
    • illnessName(varchar(255))
  • symptom

    • symptomId (int(11), AUTO_INCREMENT)
    • symptomName (varchar(255))
  • illness_symptom

    • illnessId (from the illness table)
    • symptomId (from the symptom table)

This database design makes sure you dont get multiple values in 1 row and don't have the same values BUT like I said, I don't know how to "search" or "filter" illnesses based on values from the other table. Let alone having multiple symptoms to check on... seems impossible!

I've got this query though, so I can retrieve everything when the user did not input anything:

"SELECT illness.illnessId, illness.illnessName, symptom.symptomName FROM illness_symptom
 JOIN illness
 ON illness.illnessId = illness_symptom.illnessId
 JOIN symptom
 ON symptom.symptomId = illness_symptom.symptomId";

Which gives me this: image

Like I said, I only want to output "Whiplash" when the url looks like this:/nameofmyapi/data?symptoms=dizzyness+headache

Help would be appreciated!

Aucun commentaire:

Enregistrer un commentaire