这个数据库查询有什么问题?

发布于 2024-08-24 16:17:34 字数 670 浏览 4 评论 0原文

我的数据库中有以下表格(我将仅列出重要属性):

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 技术交流群。

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

发布评论

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

评论(8

把回忆走一遍 2024-08-31 16:17:34

问题是您的子查询返回多个结果,因此您必须使用 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 = to where ssn in, and where companyid = to where companyid in.

静待花开 2024-08-31 16:17:34

尝试使用 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');

许久 2024-08-31 16:17:34

使用:

SELECT c.name
  FROM COMPANY c
  JOIN EMPLOYMENT e ON e.companyid = c.companyid
  JOIN PERSON p ON p.ssn = e.ssn
               AND p.countryofbirth = 'Mexico'

Use:

SELECT c.name
  FROM COMPANY c
  JOIN EMPLOYMENT e ON e.companyid = c.companyid
  JOIN PERSON p ON p.ssn = e.ssn
               AND p.countryofbirth = 'Mexico'
脸赞 2024-08-31 16:17:34

您应该在 where 条件中使用 In,因为 (SELECT ssn
来自人
WHERE countryofbirth = 'Mexico');
可能会返回多个 ssn 值。

SELECT name 
FROM Company 
WHERE companyid = (SELECT companyid  
                   FROM Employment 
                   WHERE ssn IN (SELECT ssn 
                                FROM Person 
                                WHERE countryofbirth = 'Mexico');

You should use In in the where condition since the (SELECT ssn
FROM Person
WHERE countryofbirth = 'Mexico');
may return multiple ssn values.

SELECT name 
FROM Company 
WHERE companyid = (SELECT companyid  
                   FROM Employment 
                   WHERE ssn IN (SELECT ssn 
                                FROM Person 
                                WHERE countryofbirth = 'Mexico');
℡Ms空城旧梦 2024-08-31 16:17:34

尝试使用 IN 而不是 =

编写时:

select a from T where a = ( select....)

子查询必须返回单个值。如果它返回多个值,您会收到错误。

为了解决这个问题,我们使用 IN 运算符,它允许子查询返回一组值 (>=0),并且如果 a 等于这些值中的任何一个,则 where 条件成功。

select a from T where a IN ( select....)

Try using IN instead of =

When you write:

select a from T where a = ( select....)

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.

select a from T where a IN ( select....)
究竟谁懂我的在乎 2024-08-31 16:17:34

看看这是否有效

从人员中选择 c.Name p
LEFT JOIN 就业 e ON p.ssn=e.ssn
LEFT JOIN 公司 c ON
e.CompanyID=c.CompanyID WHERE
p.countryofbirth=

See if this works

SELECT c.Name FROM PERSON p
LEFT JOIN Employment e ON p.ssn=e.ssn
LEFT JOIN Company c ON
e.CompanyID=c.CompanyID WHERE
p.countryofbirth=

默嘫て 2024-08-31 16:17:34

该错误是由于两个子查询之一返回多行所致。例如,我认为可能有多个人出生在墨西哥。

Select Name
From  Companies
Where Exists(
            Select 1
            From Employment
                Join Person
                    On Person.SSN = Employment.SSN
                Join Parents
                    On Parents.SSN = Person.SSN
            Where Parents.FatherBirthCountry = Person.CountryOfBirth
                And Parents.FatherBirthCountry = @InputParam
                And Employment.CompanyId = Companies.CompanyId
            )

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.

Select Name
From  Companies
Where Exists(
            Select 1
            From Employment
                Join Person
                    On Person.SSN = Employment.SSN
                Join Parents
                    On Parents.SSN = Person.SSN
            Where Parents.FatherBirthCountry = Person.CountryOfBirth
                And Parents.FatherBirthCountry = @InputParam
                And Employment.CompanyId = Companies.CompanyId
            )
星星的軌跡 2024-08-31 16:17:34

理想情况下,使用 OMG Ponies 使用 加入
但是,如果您出于某种原因不喜欢 JOIN,那么 TOP 1 应该可以满足您的要求:

SELECT  name 
FROM    Company 
WHERE   companyid =(SELECT  TOP 1 companyid  
                    FROM    Employment 
                    WHERE   ssn = ( SELECT  TOP 1 ssn 
                                    FROM    Person 
                                    WHERE   countryofbirth = 'Mexico');

Ideally use the answer from OMG Ponies using JOINs.
But if you do not like JOINs for whatever reason, then TOP 1 should do the trick for you:

SELECT  name 
FROM    Company 
WHERE   companyid =(SELECT  TOP 1 companyid  
                    FROM    Employment 
                    WHERE   ssn = ( SELECT  TOP 1 ssn 
                                    FROM    Person 
                                    WHERE   countryofbirth = 'Mexico');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文