简体   繁体   English

左外连接子查询?

[英]Left Outer Join with subqueries?

----------
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. 其中ProjEmpID,ProjEmpMGRID是user_id,ProjEmpMGRID可以为null。 I need to look up the useremail and display the table project_account. 我需要查找useremail并显示表project_account。 I need to query with actNo which has duplicate values. 我需要使用具有重复值的actNo进行查询。

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: 我在Oracle中收到的错误消息:

ora-01427 single row subquery returns more than one row ora-01427单行子查询返回多行

As my subquery returns more than one email id, I get this error. 当我的子查询返回多个电子邮件ID时,我收到此错误。 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. 我在子查询中执行左外连接,因为prog manager id中可能有空值。

Any help would be appreciated. 任何帮助,将不胜感激。

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. 您得到的错误是您的一个子查询(对于project_manager或program_manager)根据您的条件为您提供了多个ID。 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) 这是有道理的,因为多个项目帐户可能具有相同的“actno”,因为您没有将其指定为Primarky Key(pk)

furhter, rather than using subqueries, just join directly to the user tables to find the IDs 而不是使用子查询,只需直接连接到用户表以查找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'

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. 这样,您就不会运行子查询并返回多个结果并尝试将它们存储为一个值。

你为什么不简单地使用它?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM