How do i insert into a table using a select statement. The first_name, last_name, and id_no are from the staff table, but i want to insert the value 1 into the department table as well, but it is not from the staff table.
INSERT INTO department ('1', id_no, first_name, last_name)
SELECT FROM staff
WHERE id_no = 1;
I am using SQL developer in oracle
If "department" has those four columns in that order, you are pretty close.
INSERT INTO department (foo, id_no, first_name, last_name)
SELECT 1, staff_id, staff_first_name, staff_last_name
FROM staff
WHERE id_no = 1;
(obviously change the column names appropriately in the SELECT
) Of course, this will insert only one row, assuming that id_no
is unique.
INSERT INTO department(OTHER_COLUMN,id_no, first_name, last_name)
SELECT '1', id_no, first_name, last_name FROM staff WHERE id_no = 1
replace OTHER_COLUMN with the name of the column
That's invalid SQL. Assuming that staff
has the same field names as department
INSERT INTO department(id_no, first_name, last_name)
SELECT id_no, first_name, last_name
FROM staff
WHERE id_no = 1;
To INSERT INTO
table values from SELECT
you need to write something like:
INSERT INTO department (dep_field1, dep_field2, dep_field3)
SELECT '1', staff_field1, staff_field2
FROM staff
WHERE condition;
In department (...)
part you need name fields from department
table, you want to insert into.
In SELECT
you need to name fields from staff
table, you want to select from.
I Think you would find an answer to your problem here: INSERT Statement
if the SQL you have added to the post is what you tried - it's not working because it says FROMM
- double M. Change it to FROM
and it might work...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.