这个数据库查询有什么问题?
我的数据库中有以下表格(我将仅列出重要属性):
Person(ssn,countryofbirth)
Parents(ssn,fatherbirthcountry)
Employment(ssn, companyID)
Company(companyID, name)
我的任务是这样的:给定父亲出生国家作为输入,输出其出生国家与父亲出生国家输入相匹配的人员工作的公司名称。
我假装父亲出生的国家是墨西哥并这样做:
SELECT name
FROM Company
WHERE companyid = (SELECT companyid
FROM Employment
WHERE ssn = (SELECT ssn
FROM Person
WHERE countryofbirth = 'Mexico');
但这给了我一个错误:
>Scalar subquery is only allowed to return a single row.
我完全偏离了轨道吗?有人可以帮忙吗?
I have the following tables in a database (i'll only list the important attributes):
Person(ssn,countryofbirth)
Parents(ssn,fatherbirthcountry)
Employment(ssn, companyID)
Company(companyID, name)
My task is this: given fatherbirthcountry as input, output the names of companies where persons work whose countryofbirth match the fatherbirthcountry input.
I pretend that the fatherbirthcountry is Mexico and do this:
SELECT name
FROM Company
WHERE companyid = (SELECT companyid
FROM Employment
WHERE ssn = (SELECT ssn
FROM Person
WHERE countryofbirth = 'Mexico');
but it is giving me an error:
>Scalar subquery is only allowed to return a single row.
am I completely off track? Can anybody please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
问题是您的子查询返回多个结果,因此您必须使用
where in
与=
。将
where ssn =
更改为where ssn in
,将where companyid =
更改为where companyid in
。The problem is that your subqueries are returning multiple results, so you have to use
where in
vs.=
.Change
where ssn =
towhere ssn in
, andwhere companyid =
towhere companyid in
.尝试使用 IN 关键字而不是“=”。
尝试将您的查询更改为此
SELECT 名称
来自公司
WHERE 公司 ID IN (SELECT 公司 ID
来自就业
WHERE ssn IN (选择 ssn
来自人
WHERE 出生国家 = '墨西哥');
try using the IN keyword not '='.
try changing your query to this
SELECT name
FROM Company
WHERE companyid IN (SELECT companyid
FROM Employment
WHERE ssn IN (SELECT ssn
FROM Person
WHERE countryofbirth = 'Mexico');
使用:
Use:
您应该在
where
条件中使用In
,因为(SELECT ssn
可能会返回多个 ssn 值。来自人
WHERE countryofbirth = 'Mexico');
You should use
In
in thewhere
condition since the(SELECT ssn
may return multiple ssn values.FROM Person
WHERE countryofbirth = 'Mexico');
尝试使用
IN
而不是=
编写时:
子查询必须返回单个值。如果它返回多个值,您会收到错误。
为了解决这个问题,我们使用 IN 运算符,它允许子查询返回一组值 (>=0),并且如果
a
等于这些值中的任何一个,则 where 条件成功。Try using
IN
instead of=
When you write:
The sub-query must return a single value. In case if it returns multiple values, you get your error.
To solve this we use the IN operator which allows the sub-query to return a set of value (>=0) and your where condition succeeds if
a
equals any one of those values.看看这是否有效
See if this works
该错误是由于两个子查询之一返回多行所致。例如,我认为可能有多个人出生在墨西哥。
The error is due to the fact that the one of the two subqueries are returning multiple rows. I would think it likely that you have multiple people born in Mexico for example.
理想情况下,使用 OMG Ponies 使用
加入
。但是,如果您出于某种原因不喜欢
JOIN
,那么TOP 1
应该可以满足您的要求:Ideally use the answer from OMG Ponies using
JOIN
s.But if you do not like
JOIN
s for whatever reason, thenTOP 1
should do the trick for you: