2012-03-30 77 views
-1

我正在嘗試爲家庭作業寫一個Oracle查詢。該任務是編寫一個查詢以返回所有員工的employee_id,job_id,hire_date和department_id,第二個查詢列出job_hist表中的employee_id,job_id,start_date和department_id,並將結果組合爲單個輸出。確保你在輸出中抑制重複。FROM關鍵字找不到預期的地方ERROR

代碼,我:

SELECT employee_id AS "Employee ID", job_id AS "Job Id", TO_CHAR(NULL) hire_date "Hire  Date", department_id AS "Department Id", 
FROM employees 
UNION 
SELECT employee_id AS "Employee Id", job_id AS "Job Id", TO_CHAR(NULL) start_date "Start  Date",department_id AS "Department Id", 
FROM job_history; 

錯誤我得到:

ORA-00923: FROM keyword not found where expected 

從我可以在書中告訴,序列大致正確。任何幫助將是偉大的!除去逗號之後

代碼:

SELECT employee_id AS "Employee ID", job_id AS "Job Id", TO_CHAR(NULL) hire_date "Hire  Date", department_id AS "Department Id" 
FROM employees 
UNION 
SELECT employee_id AS "Employee Id", job_id AS "Job Id", TO_CHAR(NULL) start_date "Start  Date",department_id AS "Department Id" 
FROM job_history; 
+1

我只想在這裏評論。在'FROM'之前有一個額外的逗號。 – cctan 2012-03-30 01:26:46

回答

1
SELECT employee_id AS "Employee Id", job_id AS "Job Id", TO_CHAR(hire_date,'yyyy-mm-dd') AS "Some date", department_id AS "Department Id" 
FROM employees 
UNION 
SELECT employee_id AS "Employee Id", job_id AS "Job Id", TO_CHAR(start_date,'yyyy-mm-dd') AS "Some date",department_id AS "Department Id" 
FROM job_history; 

這方面的工作?

+0

我有一個彈出框要求我輸入綁定變量? – 2012-03-30 01:42:47

+0

@KevinSchultz如果你輸入什麼會發生什麼? – cctan 2012-03-30 01:48:35

+0

這工作,但我不得不將日期格式更正爲'yyyy-mm-dd'。謝謝! – 2012-03-30 01:51:28

3

FROM關鍵字(UNION Syntax)之前取出逗號。

SELECT employee_id AS "Employee ID", 
     job_id AS "Job Id", TO_CHAR(NULL) hire_date "Hire  Date", department_id AS "Department Id" 
FROM employees .... 
+0

我刪除了兩個逗號,並得到了相同的錯誤。 @cctan – 2012-03-30 01:36:00

+0

@Kevin,在這種情況下,請張貼您的新代碼。 – 2012-03-30 01:37:24

-1
Select hire_date, employee_id, TO_DATE(null) start_date, 
     job_id, department_id 
from employees 
Union 
Select TO_DATE(null) hire_date, employee_id, start_date, 
     job_id, department_id 
from job_history; 
-1

SELECT僱員,JOB_ID,聘用日期,部門標識FROM 員工 UNION ALL SELECT僱員,JOB_ID,起始日期,部門標識FROM JOB_HISTORY ORDER BY EMPLOYEE_ID

+1

2012年提出了這個問題,已經有了一個可接受的答案。 – 2016-10-24 21:38:15

相關問題