简体   繁体   English

具有连接表的高级查询和来自GET参数的多个值

[英]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. 我正在创建一个API,根据他们提交的症状,它会输出用户可能患有的疾病。

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. 我希望用户输入一些内容,这会进入url,因为“症状= ...喜欢:... / nameofmyapi / data?症状=头痛+头晕。我从GET参数中检索数据,但我绝对没有关于如何输出疾病名称的想法。

My database looks like this: 我的数据库看起来像这样:

  • illness 疾病

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

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

    • illnessId (from the illness table) diseaseId(来自疾病表)
    • 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 就像我说的那样,当网址看起来像这样时我只想输出“Whiplash”:/ nameofmyapi / data?症状=晕眩+头痛

Help would be appreciated! 帮助将不胜感激!

First off all: To only output 'Whiplash' is not going to work because your other illness is also connected to the symptom 'dizzyness'. 首先关闭所有:只输出'鞭打'是行不通的,因为你的其他疾病也与症状'晕眩'有关。

I've created a database with your structure (only renamed the fields to have more default convention. Would also look at your table names to pluralfy them) and came up with this query: 我已经用你的结构创建了一个数据库(只重命名字段以具有更多默认约定。还会查看你的表名称以复制它们)并提出了这个查询:

SELECT i.id, i.name
FROM illness_symptom AS ils
JOIN illness AS i ON i.id = ils.illness_id
JOIN symptom AS s ON s.id = ils.symptom_id
WHERE s.name IN ('dizzyness',  'headache')
GROUP BY i.name

The IN part searches through your table and filters out all the other results with different symptoms. IN部分搜索您的表并过滤掉具有不同症状的所有其他结果。 You could convert the result from your GET query to a comma separated list used in the query. 您可以将GET查询的结果转换为查询中使用的逗号分隔列表。

The GROUP BY part makes sure you won't get any double results. GROUP BY部分确保您不会获得任何双重结果。 Off course you need to make sure a result only exists in your DB once (no double records with different case like 'whiplaSh'). 当然,您需要确保结果只存在于您的数据库中一次(没有双重记录,例如'whiplaSh')。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM