我在数据库中有以下表格(我只会列出重要的属性):
Person(ssn,countryofbirth)
Parents(ssn,fatherbirthcountry)
Employment(ssn, companyID)
Company(companyID, name)
我的任务是这样的:将 fatherbirthcountry 作为输入,输出出生国与 fatherbirthcountry 输入相匹配的人员所在公司的名称。
我假设 fatherbirthcountry 是墨西哥,然后这样做:
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.
我完全偏离轨道了吗?有人可以帮忙吗?
最佳答案
问题是您的子查询返回了多个结果,因此您必须使用 where in
与 =
。
将 where ssn =
更改为 where ssn in
,将 where companyid =
更改为 where companyid in
。
关于sql - 这个数据库查询有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2430629/