2013-10-01 63 views
0

當我在sql * plus中運行此查詢時,出現ORA-00923(未找到FROM關鍵字)的錯誤錯誤。SQL * PLUS中的查詢問題

SELECT EMPLOYEE_ID, FIRST_NAME||' '||LAST_NAME AS FULLNAME 
FROM EMPLOYEES 
WHERE (JOB_ID, DEPARTMENT_ID) 
IN (SELECT JOB_ID, DEPARTMENT_ID FROM JOB_HISTORY) 
AND DEPARTMENT_ID=80; 

我跑在SQL Developer中查詢和猜測什麼,它的工作沒有任何問題,當我嘗試在SQL * Plus爲什麼我收到此錯誤信息。

+2

只是一個查詢:你爲什麼在你的where子句中使用'DEPARTMENT_ID'兩次?如果'DEPARTMENT_ID'必須是'80',那麼您可以在內部查詢中檢索'job_id'? – heretolearn

+0

在SQL Plus客戶端中,您可以將查詢分解爲多行來隔離問題。 – heretolearn

+0

我知道,但我只是練習子查詢主題。我沒有這本書的相同數據庫,所以我讓這個查詢來練習。 xDDDDD –

回答

1
SELECT EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME AS FULLNAME 
    FROM EMPLOYEES 
WHERE JOB_ID IN (SELECT JOB_ID 
         FROM JOB_HISTORY 
        WHERE DEPARTMENT_ID = 80); 

OR

SELECT EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME AS FULLNAME 
    FROM EMPLOYEES 
WHERE JOB_ID IN (SELECT JOB_ID FROM JOB_HISTORY) AND DEPARTMENT_ID = 80; 

OR

SELECT EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME AS FULLNAME 
    FROM EMPLOYEES E 
WHERE EXISTS (SELECT NULL 
        FROM JOB_HISTORY J 
        WHERE J.JOB_ID = E.JOB_ID) 
     AND DEPARTMENT_ID = 80; 
+0

這比我的答案要好。 +1 –

0

我不知道你想達到什麼樣的,但這裏有一個可能的解決方案:

/* Formatted on 10/1/2013 1:50:20 PM (QP5 v5.126.903.23003) */ 
SELECT EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME AS FULLNAME 
    FROM  EMPLOYEES EMP 
     JOIN 
      JOB_HISTORY JH 
     ON EMP.JOB_ID = JH.JOB_ID AND EMP.DEPARTMENT_ID = JH.DEPARTMENT_ID 
WHERE EMP.DEPARTMENT_ID = 80; 
+2

''我不知道你在努力達成什麼目標,但這裏有一個可能的解決方案''讓我微笑。沒有進攻,抱歉) –

+0

@beherenow,這個問題本身並沒有解釋他真正想要達到的目標。對不起。= D –

+1

只要練習子查詢=) –

1

您的查詢完全有效,完全按照它應該在sqlplus中運行:

14:04:01 (41)[email protected]> l 
    1 SELECT EMPLOYEE_ID, FIRST_NAME||' '||LAST_NAME AS FULLNAME 
    2 FROM EMPLOYEES 
    3 WHERE (JOB_ID, DEPARTMENT_ID) 
    4 IN (SELECT JOB_ID, DEPARTMENT_ID FROM JOB_HISTORY) 
    5* AND DEPARTMENT_ID=80 
14:04:05 (41)[email protected]>/

34 rows selected. 

Elapsed: 00:00:00.01 

只有當您遇到語法錯誤時纔會遇到ORA-00923。像這樣:

14:04:06 (41)[email protected]> ed 
Wrote file S:\spool\sandbox\BUF_HR_41.sql 

    1 SELECT EMPLOYEE_ID, FIRST_NAME||' '||LAST_NAME AS FULLNAME X 
    2 FROM EMPLOYEES 
    3 WHERE (JOB_ID, DEPARTMENT_ID) 
    4 IN (SELECT JOB_ID, DEPARTMENT_ID FROM JOB_HISTORY) 
    5* AND DEPARTMENT_ID=80 
14:05:17 (41)[email protected]>/
SELECT EMPLOYEE_ID, FIRST_NAME||' '||LAST_NAME AS FULLNAME X 
                  * 
ERROR at line 1: 
ORA-00923: FROM keyword not found where expected 

可能你在從sqldeveloper複製你的查詢到sqlplus時做了一個?你確定你的文章中包含了符號 - 符號嗎?你實際上試圖執行的查詢嗎?我會更加關注查詢文本和錯誤信息 - 它通常指向一個錯誤,例如*,在我的示例中爲X

+0

謝謝你的提問。也許這是一個語法錯誤,我沒有看到,我再次運行查詢,它的工作。 –