2013-02-12 73 views
8
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')) 

给出子查询返回多个行的错误。原因是什么?子查询返回多于一行

+1

我想我会是一个明显的例子:子查询返回多行。 'disease_id'不能等于多个值。这个查询最好用'JOIN'来代替子查询。 – 2013-02-12 21:17:30

+0

http://stackoverflow.com/a/3423792/2806972 c这可能会得到这个Q的解决方案: – 2014-04-05 08:03:04

回答

2

打破查询下来,你有

主查询:

select disease_name from disease where disease_id= 

子查询1:

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

子查询2:

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

由于您使用等号,子查询cann ot返回多个项目。看起来子查询2由于使用了OR而有更大的机会返回2个项目。您不妨尝试IN子句,如WHERE symptom_id IN (sub-query2)WHERE disease_id IN (sub-query1)

14

您的两个外部查询的结构是希望从其子查询得到单个结果。但是,如果你有结构化的东西,你的子查询可能会返回多个结果。如果你真的想不止一个结果,重组这样的:

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

此外,子查询是杀人的性能,它的成倍wosrse为嵌套子查询。您可能需要考虑使用INNER JOIN