帮助子查询!返回多于 1 行

发布于 2024-08-25 08:45:25 字数 705 浏览 1 评论 0原文

我不明白返回多行的问题:

这是我的表 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.............................

问题:

列出国家名称和地区 在包含“印度”的地区, “伊朗”。

这是我的声明:

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

它返回:

sql: errorSubquery returns more than 1 row

我的声明有什么问题吗?答案应该是选择语句内的选择语句的形式,

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最冷一天 2024-09-01 08:45:26

这是因为您尝试将 region 与值表进行比较。相反,尝试使用 in

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

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')
寄人书 2024-09-01 08:45:26

您的语法可能略有不同,但它会起作用:

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

唯一的区别是我们使用 IN 而不是等于 (=)。

前一个失败的原因是因为使用 equals 时,您需要将一个值与另一个值进行比较。您不小心所做的是将一个值与多个值进行比较(“子查询返回多行”)。这里的变化是说 region 位于子查询返回的结果中。

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.

自此以后,行同陌路 2024-09-01 08:45:26

select name,region from bbc whereregion IN (select region from bbc where name IN('印度','伊朗'))

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文