2014-10-08 473 views
1

我试图插入的行数的表,其中值从选择查询如何使用循环插入PostgreSQL中

insert into article_rel (x_id, y_id) VALUES (12, (SELECT id from table where name = 'string') 

这应该是一样执行该

insert into article_rel (x_id, y_id) VALUES (12, 1) 
insert into article_rel (x_id, y_id) VALUES (12, 3) 
insert into article_rel (x_id, y_id) VALUES (12, 4) 
来到

等。

我看到这个How to use a SQL for loop to insert rows into database?,但我不知道怎么能这样帮助我

由于提前,

回答

4

使用此查询:

INSERT INTO article_rel (x_id, y_id) 
    SELECT 12, id 
    FROM table_name 
    WHERE name = 'string'