3
----------
User
----------
user_ID(pk)
UserEmail

----------
Project_Account
----------
actno
actname
projno
projname
ProjEmpID
ProjEmpMGRID

其中 ProjEmpID,ProjEmpMGRID 是 user_id,ProjEmpMGRID 可以为空。我需要查找 useremail 并显示表 project_account。我需要使用具有重复值的 actNo 进行查询。

我的查询是这样的:

 select projno,projname,actno,actname,
(select u.user_email as project_manager from project_account c left outer join users u
     on u.user_id = c.ProjEmpID where actno='some no')as project_manager,

     (select u.user_email as program_manager from project_account c left outer join users u
        on u.user_id = c.ProjEmpMGRID where actno='someno') as program_manager

        from project_account where actno='someno'

我在 Oracle 中收到的错误消息:

ora-01427 单行子查询返回多于一行

由于我的子查询返回多个电子邮件 ID,我收到此错误。正如我所说,不行动并不是唯一的。我可以理解错误,但我无法找出解决方案。我在子查询中进行左外连接,因为 prog manager id 中可能有空值。

任何帮助,将不胜感激。

4

3 回答 3

5

您得到的错误是您的子查询之一(对于 project_manager 或 program_manager)根据您的条件为您返回多个 ID。这是有道理的,因为多个项目帐户可能具有相同的“actno”,因为您没有将其指定为 Primarky Key (pk)

进一步,而不是使用子查询,只需直接加入用户表以查找 ID

 select projno,projname,actno,actname,
  project_user.user_email as project_manager,
  program_user.user_email as program_manager
    from project_account 
    left join User as project_user
      on project_account.ProjEmpID = project_user.user_id
    left join User as program_user
      on project_account.ProjEmpMGRID = program_user.user_id

where actno='someno'
于 2010-08-30T19:41:53.333 回答
2

怎么样的东西:

select c.projno, c.projname, c.actno, c.actname, u.user_email as project_manager, us.user_email as program_manager

from project_account c

left outer join users u
on u.user_id = c.ProjEmpID

left outer join users us
on us.user_id = c.ProjEmpMGRID

WHERE actno = 'someno'

这样,您就不会运行子查询并返回多个结果并尝试将它们存储为一个值。

于 2010-08-30T19:43:15.883 回答
0

你为什么不简单地使用这个?

select projno, projname, actno, actname, (select user_email from users where user_id = pa.projempid), (select user_email from users where user_id = pa.projempmgrid) from project_account pa

于 2013-08-12T15:14:47.100 回答