簡體   English   中英

Postgres-僅從一個表的另一張表插入數據

[英]Postgres - Inserting data from another table for one column only

我正在Postgres 9.6.3中嘗試以下查詢

INSERT INTO join_table ("table_1_id", "table_2_id") VALUES
      (7, SELECT "id" from "table_2" where label='Foo Bar');

這將引發ERROR: syntax error at or near "SELECT" at character 94

我已經看到了插入語句工作中的嵌套選擇的示例,其中僅插入被選擇的內容。 上面的查詢是否可能?

嘗試在子查詢周圍添加括號:

INSERT INTO join_table ("table_1_id", "table_2_id") VALUES
      (7, (SELECT "id" from "table_2" where label='Foo Bar'));

使用insert . . . select insert . . . select insert . . . select values是不必要的:

INSERT INTO join_table (table_1_id, table_2_id)
    SELECT y, "id" 
    FROM "table_2" 
    WHERE label = 'Foo Bar';

這還允許您從另一個表插入多行。

暫無
暫無

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

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