subquery returns more than 1 row

回眸只為那壹抹淺笑 提交于 2019-11-30 03:00:18

问题


select 
    disease_name 
from 
    disease 
where 
    disease_id=
    (select disease_id from disease_symptom where
        disease.disease_id=disease_symptom.disease_id AND 
        symptom_id=
               (select symptom_id from symptom where symptom.symptom_id=disease_symptom.symptom_id
                AND symptom_name='fever' OR symptom_name='head ache'))

Gives an error that subquery returns more than one row. what is the cause?


回答1:


Your two outer queries are structured to expect a single result from the their subqueries. But the way you have things structured, your subqueries might return more than one result. If you actually want more than one result, restructure it like this:

... where disease_id IN (subquery returning multiple rows...)

Also, subqueries is kill performance, and it's exponentially wosrse for nested subqueries. You might want to look into using INNER JOIN instead.




回答2:


Breaking your query down, you have

Main query:

select disease_name from disease where disease_id=

Subquery 1:

select disease_id from disease_symptom where
        disease.disease_id=disease_symptom.disease_id AND 
        symptom_id=

Sub query 2:

select symptom_id from symptom where symptom.symptom_id=disease_symptom.symptom_id
            AND symptom_name='fever' OR symptom_name='head ache'

Since you are using equal signs, the subqueries cannot return multiple items. It looks like sub query 2 has a greater chance to return 2 items due to the OR being used. You may wish to try IN clause such as WHERE symptom_id IN (sub-query2) with WHERE disease_id IN (sub-query1)



来源:https://stackoverflow.com/questions/14841945/subquery-returns-more-than-1-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!