简单的SQL表插入

时间:2013-11-19 02:50:47

标签: sql oracle insert

我希望从下面的查询中获取数据(西雅图的所有员工)并将其插入到一个新的空白表中,其中列与我的select语句相同。

select e.employee_id, e.first_name, e.last_name, e.email, e.phone_number, e.hire_date, e.job_id,       e.salary, e.commission_pct, e.manager_id, e.department_id
from employees e
join departments d
on e.department_id = d.department_id
join jobs j
on e.job_id = j.job_id
join locations l
on d.location_id = l.location_id
where l.city = 'Seattle';

知道怎么做吗?

3 个答案:

答案 0 :(得分:2)

如果您有现有的表,您可以这样做:

INSERT INTO ExistingTable (Columns,..)
SELECT Columns,...
FROM OtherTable

从你的sql

insert into newEmpTable (employee_id, first_name, 
  last_name, email, phone_number, hire_date, 
  job_id, salary, commission_pct, manager_id, department_id)
select e.employee_id, e.first_name, e.last_name, e.email, e.phone_number, e.hire_date, e.job_id,       e.salary, e.commission_pct, e.manager_id, e.department_id
from employees e
join departments d
on e.department_id = d.department_id
join jobs j
on e.job_id = j.job_id
join locations l
on d.location_id = l.location_id
where l.city = 'Seattle';

请参阅http://docs.oracle.com/cd/E17952_01/refman-5.1-en/insert-select.html

如果您没有桌子并且想要创建它,

create table new_table as 
select e.employee_id, e.first_name, e.last_name, e.email, e.phone_number, e.hire_date,     e.job_id,       e.salary, e.commission_pct, e.manager_id, e.department_id
from employees e
join departments d
on e.department_id = d.department_id
join jobs j
on e.job_id = j.job_id
join locations l
on d.location_id = l.location_id
where l.city = 'Seattle';

答案 1 :(得分:1)

CREATE TABLE SeatleEmployees AS
(/* your query here */);

答案 2 :(得分:0)

    CREATE VIEW seattleView AS
    /*YOUR SELECT STATEMENT HERE*/;

然后

    CREATE TABLE newTbl AS
    SELECT /*ALL THE COLUMN NAMES*/
    FROM seattleView;