使用自定义数据从另一个表插入表

时间:2015-09-13 06:54:39

标签: sql postgresql select sql-insert

当我阅读here如果我想在PostgreSQL数据库中插入另一个表时我必须使用它:

此查询插入另一个表中的某些行

INSERT INTO books (id, title, author_id, subject_id)
       SELECT nextval('book_ids'), title, author_id, subject_id
              FROM book_queue WHERE approved;

但是如何使用我的自定义数据(默认或非默认)插入包含更多列的行?

,例如books (id, title, author_id, <a cell with my data1>,subject_id,<a cell with my data2>)

如何在插入表格之前更新所选数据?

是的,例如:

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;

1 个答案:

答案 0 :(得分:1)

插入插入行,并选择generate / forms 行,因此如果您可以使用任何select语句构建要插入的数据,则可以插入它,即使存在是新字段或更新字段。例如:

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;

以下是fiddle