简体   繁体   English

无法从子查询中选择列

[英]Cannot select a column from a subquery

I made this query that i need but i need only one column from the result. 我做了我需要的查询,但是我只需要从结果中选择一列。 So it goes like this: 所以它是这样的:

select SWDATECREATED from 
      (select * from MBR_INST_PRODUCTS
       inner join MBR_SUBSCRIBERS 
       on MBR_INST_PRODUCTS.SWOBJECTID = MBR_SUBSCRIBERS.SWSUBRID 
       where MBR_SUBSCRIBERS.SWSUBRID = 54501928
       and SWINSTPRODID = 1433193947);

If i run the select within the brackets it`s good. 如果我在方括号内运行select,那就很好。 But if i try to select only a specific column from that result set i get error ORA-00904. 但是,如果我尝试从该结果集中仅选择特定列,则会收到错误ORA-00904。 I checked the error already but all the columns are written correctly. 我已经检查了错误,但所有列均已正确写入。 How can i select only SWDATECREATED from the select? 如何从选择中仅选择SWDATECREATED?

Here`s my error: 这是我的错误:
ORA-00904: "SWDATECREATED": invalid identifier 00904. 00000 - "%s: invalid identifier" *Cause: ORA-00904:“ SWDATECREATED”:无效的标识符00904。00000-“%s:无效的标识符” *原因:
*Action: Error at Line: 5 Column: 8 *操作:第5行错误:8列

Thank you for your time. 感谢您的时间。

You basically have a situation like this, where you have the same column names in both tables (though presumably you have more columns than this): 基本上,您会遇到这样的情况,即两个表中的列名都相同(尽管可能您的列数比这还多):

create table MBR_INST_PRODUCTS (SWINSTPRODID, SWOBJECTID, SWDATECREATED) as
select 1433193947, 54501928, date '2018-01-01' from dual;

create table MBR_SUBSCRIBERS (SWSUBRID, SWDATECREATED) as
select 54501928, date '2018-02-28' from dual;

select * from MBR_INST_PRODUCTS
inner join MBR_SUBSCRIBERS 
on MBR_INST_PRODUCTS.SWOBJECTID = MBR_SUBSCRIBERS.SWSUBRID 
where MBR_SUBSCRIBERS.SWSUBRID = 54501928
and SWINSTPRODID = 1433193947;

SWINSTPRODID SWOBJECTID SWDATECREATED         SWSUBRID SWDATECREATED      
------------ ---------- ------------------- ---------- -------------------
  1433193947   54501928 2018-01-01 00:00:00   54501928 2018-02-28 00:00:00

In the result set you have all the columns from both tables, including those with the same name. 在结果集中,您拥有两个表中的所有列,包括具有相同名称的列。 The client (SQL Developer in this case) is displaying the original column names, but some might change them to be unique. 客户端(在本例中为SQL Developer)正在显示原始列名,但有些可能会将其更改为唯一。

When you try to use that query as an inline view the names have to be unique, and Oracle is implicitly adding something internally to make them unique; 当您尝试使用该查询作为内联视图时,名称必须是唯一的,Oracle会在内部隐式添加一些内容以使其唯一。 it doesn't really matter what or how it decides. 决定什么或如何决定并不重要。 When you do 当你做

select SWDATECREATED from ( .. that query ... )

neither internal name for those two columns in the inline view now matches the one you expect to see, so you get an ORA-00904. 现在,内联视图中这两列的内部名称都与您希望看到的名称不匹配,因此得到ORA-00904。 That identifier does not exist in the inline view. 内联视图中存在该标识符。

You don't need a subquery to get just that column, but you have to specify which of the two columns you want - otherwise you'll get an ORA-00918 since Oracle can't guess which one you meant: 您不需要子查询来仅获得该列,但是您必须指定想要的两列中的哪一列-否则您将获得ORA-00918,因为Oracle无法猜测您的意思是:

select MBR_INST_PRODUCTS.SWDATECREATED from MBR_INST_PRODUCTS
inner join MBR_SUBSCRIBERS 
on MBR_INST_PRODUCTS.SWOBJECTID = MBR_SUBSCRIBERS.SWSUBRID 
where MBR_SUBSCRIBERS.SWSUBRID = 54501928
and SWINSTPRODID = 1433193947;

SWDATECREATED      
-------------------
2018-01-01 00:00:00

select MBR_SUBSCRIBERS.SWDATECREATED from MBR_INST_PRODUCTS
inner join MBR_SUBSCRIBERS 
on MBR_INST_PRODUCTS.SWOBJECTID = MBR_SUBSCRIBERS.SWSUBRID 
where MBR_SUBSCRIBERS.SWSUBRID = 54501928
and SWINSTPRODID = 1433193947;

SWDATECREATED      
-------------------
2018-02-28 00:00:00

If you have another reason to use a subquery then you either need to specify which table you want that column from in the same way, optionally including other columns; 如果您还有其他原因要使用子查询,则需要以相同的方式指定要从哪个表中获取该列,或者可以选择包括其他列; or include both columns but give them unique aliases. 或同时包含这两列,但为其赋予唯一的别名。 You will then be able to refer to the alises in the outer query. 然后,您将能够在外部查询中引用别名。


It's often considered bad practice to use * , particularly in a subquery, partly because it can cause more work than necessary to be done, partly because you can be tripped up by table changes between code runs, and partly because it can cause this sort of confusion. 使用*常常被认为是不好的做法,尤其是在子查询中,部分原因是它可能导致不必要的工作,部分原因是代码运行之间的表更改可能使您不满意,部分原因是它可能导致这种情况。混乱。

It's better to explicitly name the columns you want; 最好显式命名所需的列。 and to prefix all columns with the table they are coming from (or a table alias) even if they are unique, so you and anyone who has to maintain the code can follow it more easily; 并且为所有列加上它们来自的表(或表别名)的前缀(即使它们是唯一的),因此您和任何需要维护代码的人都可以更轻松地遵循它; I had to guess which table to put the SWINSTPRODID column in. It's also safer - just in case a new column is added to a table later which makes it non-unique. 我不得不猜测将SWINSTPRODID列放入哪个表。它也更安全-以防万一稍后将新列添加到表中使其不唯一。

You should just use that as columnname in the query: 您只应在查询中将其用作列名:

select SWDATECREATED from MBR_INST_PRODUCTS
   inner join MBR_SUBSCRIBERS 
   on MBR_INST_PRODUCTS.SWOBJECTID = MBR_SUBSCRIBERS.SWSUBRID 
   where MBR_SUBSCRIBERS.SWSUBRID = 54501928
   and SWINSTPRODID = 1433193947;

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

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