简体   繁体   English

子查询返回超过1行

[英]subquery returns more than 1 row

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? 原因是什么?

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. 此外,子查询是kill性能,并且它对嵌套子查询呈指数级支持。 You might want to look into using INNER JOIN instead. 您可能希望改为使用INNER JOIN

Breaking your query down, you have 打破你的查询,你有

Main query: 主要查询:

select disease_name from disease where disease_id=

Subquery 1: 子查询1:

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

Sub query 2: 子查询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. 由于使用了OR ,子查询2看起来更有可能返回2个项目。 You may wish to try IN clause such as WHERE symptom_id IN (sub-query2) with WHERE disease_id IN (sub-query1) 您可能希望使用WHERE disease_id IN (sub-query1)尝试IN子句,例如WHERE symptom_id IN (sub-query2) WHERE disease_id IN (sub-query1)

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

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