簡體   English   中英

SQL Oracle缺少關鍵字

[英]SQL Oracle Missing keyword

create table EmployeesUK_9035
  2  as
  3  select e.employee_id,e.first_name || e.Last_name "Name",e.Salary,l.City,e.hire_date from employees e join locations l
  4  where e.employee_id in(select e.employee_id from employees e join departments d on(e.department_id=d.department_id) join locations l on(d.location_id=l.location_id) where l.city='London');

... from employees e join locations l
4 where
from employees e join locations l
4 where

您已經錯過了JOIN子句的ON部分(在主查詢中,而不是子查詢中)。

這是那種燈籠褲,應該很容易發現。 但是,由於您的代碼堆積如山,因此很難診斷。 很好地布置代碼不只是經驗豐富的開發人員精打細算:可讀性實際上是代碼的功能。 像這樣 ...

create table EmployeesUK_9035
as
select e.employee_id,
    e.first_name || e.Last_name "Name",
    e.Salary,
    l.City,
    e.hire_date 
from employees e 
     join locations l

where e.employee_id in (select e.employee_id 
                        from employees e 
                             join departments d 
                                 on (e.department_id=d.department_id) 
                             join locations l 
                                 on (d.location_id=l.location_id) 
                        where l.city = 'London')
    ;

看到發現缺失線有多容易? 您需要一個ON子句才能連接EMPLOYEES和LOCATIONS。 但是,考慮到子查詢的聯接,您可能還需要在主查詢中包括DEPARTMENTS,因為兩個表之間似乎沒有聯接。 在這種情況下,查詢可能會簡化為

create table EmployeesUK_9035
as
select e.employee_id,
    e.first_name || e.Last_name "Name",
    e.Salary,
    l.City,
    e.hire_date 
from employees e 
     join departments d 
        on (e.department_id=d.department_id) 
    join locations l 
        on (d.location_id=l.location_id) 
where l.city = 'London'
;

順便說一句,在創建表時,請不要對列別名使用雙引號和大小寫混合大小寫。 每次引用時,您都必須在雙引號中使用"Name" ,並且使用完全相同的大小寫,這很麻煩,因為Oracle代碼通常不區分大小寫。 也就是說,默認情況下,所有Oracle標識符都是大寫的,但是大小寫無關緊要,只要我們不將標識符用雙引號引起來即可。

JOIN應該打開(以顯示您要連接的表的內容),而您的沒有。

我將ON 1 = 1設置為ON 1 = 1 ,但您應該使用EMPLOYEES和LOCATIONS表中的列。

CREATE TABLE EmployeesUK_9035
AS
   SELECT e.employee_id,
          e.first_name || e.Last_name "Name",
          e.Salary,
          l.City,
          e.hire_date
     FROM employees e JOIN locations l 
                      ON 1 = 1                      --> this
    WHERE e.employee_id IN (SELECT e.employee_id
                              FROM employees e
                                   JOIN departments d
                                      ON (e.department_id = d.department_id)
                                   JOIN locations l
                                      ON (d.location_id = l.location_id)
                             WHERE l.city = 'London');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM