Oracle子选择不产生错误?

时间:2014-09-02 09:54:38

标签: oracle subquery

通常情况下,如果有问题我们都会发帖。这是不同的,我会期待一个错误,但我没有得到一个。

在Oracle中,我有以下内容:

create table temp_table as
SELECT PersonID AS OtherName
FROM Personnel
WHERE PersonID = '12345';

1. select FunnyName from temp_table;  -- This produce an error as expected - good.

2. SELECT * FROM Personnel
WHERE PersonID in (select OtherName from temp_table);  --This produces 1 record - good

3. SELECT * FROM Personnel 
WHERE PersonID in (select FunnyName from temp_table);  --This produces all records - bad

我希望语句3也会出错,因为subselect与select in语句1相同,它会产生错误。当然这不是Oracle的错误,但我不明白逻辑。

我正在使用Oracle 11。

2 个答案:

答案 0 :(得分:1)

我无法重现。这是我在HR模式中尝试时的SQL * Plus输出:

SQL> create table temp_table
  2  as
  3  select employee_id as other_id
  4  from employees
  5  where employee_id = 100;

Table created.

SQL>
SQL> select funny_id
  2  from temp_table;
select funny_id
       *
ERROR at line 1:
ORA-00904: "FUNNY_ID": invalid identifier


SQL>
SQL> select employee_id
  2  from employees
  3  where employee_id in (select other_id from temp_table);

EMPLOYEE_ID
-----------
        100

SQL>
SQL> select employee_id
  2  from employees
  3  where employee_id in (select funny_id from temp_table);
where employee_id in (select funny_id from temp_table)
                             *
ERROR at line 3:
ORA-00904: "FUNNY_ID": invalid identifier

如果您在HR示例模式中尝试此操作,您会得到相同的结果吗?


跟进San的评论:

以下是一个例子:

SQL> select employee_id
  2  from employees
  3  where employee_id in (select employee_id from temp_table);

EMPLOYEE_ID
-----------
        100
        101
        102
        103
.....
        203
        204
        205
        206

107 rows selected.

这种行为的原因是,在子查询中,解析器发现employee_id不是temp_table中的列,但 是员工中的一列。

答案 1 :(得分:0)

答案是 - 这在Oracle中是不可能的。

你确定你得到了结果吗?你可以发布你的输出吗?

我无法重现

CREATE TABLE Table1
    (a number)
;

INSERT ALL 
    INTO Table1 (a)
         VALUES (1)
SELECT * FROM dual
;

select * from table1 where a in (select b from table1);


ORA-00904: "B": invalid identifier : select * from table1 where a in (select b from table1)

在此处查看Fiddle