带子查询的左外连接?

发布于 2024-09-16 10:21:20 字数 982 浏览 7 评论 0原文

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

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

其中 ProjEmpID、ProjEmpMGRID 是 user_id,ProjEmpMGRID 可以为 null。 我需要查找 userremail 并显示表 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 中可能存在空值。

任何帮助将不胜感激。

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

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

Where ProjEmpID,ProjEmpMGRID is the user_id and ProjEmpMGRID can be null.
I need to look up the useremail and display the table project_account. I need to query with actNo which has duplicate values.

My query goes like this:

 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'

The error message I get in Oracle:

ora-01427 single row subquery returns
more than one row

As my subquery returns more than one email id, I get this error. As I said, act no is not unique. I could understand the error, but I couldn't figure out the solution. I am doing a left outer join in a subquery because there might be nulls in prog manager id.

Any help would be appreciated.

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

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

发布评论

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

评论(3

信愁 2024-09-23 10:21:20

您收到的错误是您的子查询之一(针对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'

The error you are getting is that one of your subqueries (either for project_manager or program_manager) is giving you back more than one ID based on your conditions. This kind of makes sense, since multiple project accounts could have the same "actno" since you haven't specified that as a Primarky Key (pk)

furhter, rather than using subqueries, just join directly to the user tables to find the IDs

 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'
棒棒糖 2024-09-23 10:21:20

怎么样:

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'

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

What about something like:

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'

This way you aren't running subqueries and returning multiple results and trying to store them as one value.

小矜持 2024-09-23 10:21:20

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

选择 projno、projname、actno、actname,(从 user_id = pa.projempid 的用户中选择 user_email),(从 user_id = pa.projempmgrid 的用户中选择 user_email)
来自project_account pa

Why don't you simply use this?

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

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