2014-09-02 131 views
1

通常情況下,如果有問題,我們都會發帖。這是不同的地方,我會期望一個錯誤,但我沒有得到一個。Oracle子查詢不會產生錯誤?

在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也給出錯誤的子查詢是相同的陳述1所選擇,它給出錯誤。這當然不是Oracle的錯誤,但我不明白這個邏輯。

我使用的是Oracle 11.

+0

說法是正確的2?或者它應該是SELECT * FROM Personnel WHERE PersonID(從temp_table中選擇OtherName); – 2014-09-02 10:09:13

+0

你是對的。我的道歉 - 將解決。 – 2014-09-02 10:11:56

+2

你可以發佈個人表中的列嗎?人員表中是否有任何專欄「FunnyName」?查詢2似乎是錯誤的,它應該是select * from個人表,因爲temp_table中沒有PersonID列。 – San 2014-09-02 10:17:00

回答

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樣本架構中嘗試這樣做,您會得到相同結果嗎?


跟進從聖註釋:

下面是一個例子:

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中,但它的員工中的一列。

+0

我已經重試它再次是第四次,現在它的行爲如預期的那樣(正如你所提到的那樣)。我不確定其他3次嘗試有什麼不同,但我會假設我錯過了其他的事情。謝謝您的幫助。 – 2014-09-02 11:26:14

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這裏

+0

我已經第四次重試它,現在它的行爲和預期的一樣(正如你所提到的那樣)。我不確定其他3次嘗試有什麼不同,但我會假設我錯過了其他的事情。謝謝您的幫助。 – 2014-09-02 11:25:38

+0

@ user1208908你應該發佈你所有的表結構。 – SriniV 2014-09-02 12:00:39