Reputation: 27221
As I read here If I want to insert into a table from another in PostgreSQL database I have to use this:
This query inserts some rows from another table
INSERT INTO books (id, title, author_id, subject_id)
SELECT nextval('book_ids'), title, author_id, subject_id
FROM book_queue WHERE approved;
But how can I insert a row with more columns with my custom data(default or non default)?
E.g., books (id, title, author_id, <a cell with my data1>,subject_id,<a cell with my data2>)
And how can I update a selected data before inserting into a table?
I.e., something like:
INSERT INTO books (id, title, author_id, subject_id)
SELECT nextval('book_ids'), title, author_id+1, subject_id/2
FROM book_queue WHERE approved;
Upvotes: 0
Views: 2429
Reputation: 2796
Insert inserts rows, and select generates/forms rows, so if you can build the data you want to insert with any select
statement, then you can insert it, even if there are new fields or updated fields. For example:
INSERT INTO books (title, author_id, subject_id, combined)
SELECT title, author_id+1, subject_id/2, author_id || ' ' || title
FROM book_queue WHERE approved;
Here is a fiddle to play with.
Upvotes: 1