help with subquery! returns more than 1 row

前端 未结 3 1033
慢半拍i
慢半拍i 2021-01-28 06:53

i dont understand the problem with returning multiple rows:

here is my table BBC:

name    region  area    population  gdp
Afghanistan South Asia  652225          


        
相关标签:
3条回答
  • 2021-01-28 07:20

    select name,region from bbc where region IN (select region from bbc where name IN('India','Iran'))

    0 讨论(0)
  • 2021-01-28 07:24

    You might have slightly different syntax and it'll work:

    SELECT name 
    FROM bbc 
    WHERE region IN
    (
       SELECT region FROM bbc WHERE name='India' OR name='Iran'
    )
    

    The only difference being that instead of equals (=), we use IN.

    The reason your previous one failed is because to use equals, you compare one value with one other value. What you were accidentally doing is comparing one value with multiple values (the "SubQuery returns more than one row"). The change here is saying where region is within the results returned from the sub query.

    0 讨论(0)
  • 2021-01-28 07:28

    This is because you are trying to compare region to a table of values. Instead, try using in:

    select name 
    from bbc 
    where region in 
        (select region from bbc where name='India' or name='Iran')
    
    0 讨论(0)
提交回复
热议问题