help with subquery! returns more than 1 row

99封情书 提交于 2019-12-31 06:53:28

问题


i dont understand the problem with returning multiple rows:

here is my table BBC:

name    region  area    population  gdp
Afghanistan South Asia  652225  26000000    
Albania Europe  28728   3200000 6656000000
Algeria Middle East 2400000 32900000    75012000000
Andorra Europe  468 64000   
Angola  Africa  1250000 14500000    14935000000
etc.............................

question:

List the name and region of countries in the regions containing 'India', 'Iran'.

this is my statement:

select name from bbc where region = (select region from bbc where name='India' or name='Iran')

it returns:

sql: errorSubquery returns more than 1 row

whats wrong with my statement? the answer should be in the form of a select statement within a select statement

thank you!


回答1:


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')



回答2:


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.




回答3:


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



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

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