Postgres使用SELECT查询来INSERT记录

时间:2018-02-12 21:19:32

标签: sql postgresql insert

我有一个返回大约1k结果的查询。一列orders.id

SELECT orders.id
FROM orders
LEFT JOIN uploads
ON uploads.order = orders.id
WHERE uploads.order IS NULL
AND orders.product_type = 'warrior_workout'

我也有这个INSERT查询:

INSERT INTO uploads ( "order", name, url, created_at, updated_at,  file_name, is_main ) 
VALUES 
    ( orders.id, 'warrior_workout_1','https://example.com/upload1.pdf', now(), now(), 'upload1.pdf', TRUE ),
    ( orders.id, 'warrior_workout_2','https://example.com/upload2.pdf', now(), now(), 'upload2.pdf', TRUE ),
    ( orders.id, 'warrior_workout_3','https://example.com/upload3.pdf', now(), now(), 'upload3.pdf', TRUE )

我需要为第一个查询中的每个返回的ID执行插入查询。如何将这些结合起来以获得所需的结果?

2 个答案:

答案 0 :(得分:1)

这是一个全面的例子:

t=# with i ( name, url, created_at, updated_at,  file_name, is_main ) as
(VALUES
    (  'warrior_workout_1','https://example.com/upload1.pdf', now(), now(), 'upload1.pdf', TRUE ),
    (  'warrior_workout_2','https://example.com/upload2.pdf', now(), now(), 'upload2.pdf', TRUE ),
    (  'warrior_workout_3','https://example.com/upload3.pdf', now(), now(), 'upload3.pdf', TRUE ))
select gs,i.* from i
join generate_series(1,4) gs on true order by gs, name;
 gs |       name        |               url               |          created_at           |          updated_at           |  file_name  | is_main
----+-------------------+---------------------------------+-------------------------------+-------------------------------+-------------+---------
  1 | warrior_workout_1 | https://example.com/upload1.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload1.pdf | t
  1 | warrior_workout_2 | https://example.com/upload2.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload2.pdf | t
  1 | warrior_workout_3 | https://example.com/upload3.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload3.pdf | t
  2 | warrior_workout_1 | https://example.com/upload1.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload1.pdf | t
  2 | warrior_workout_2 | https://example.com/upload2.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload2.pdf | t
  2 | warrior_workout_3 | https://example.com/upload3.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload3.pdf | t
  3 | warrior_workout_1 | https://example.com/upload1.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload1.pdf | t
  3 | warrior_workout_2 | https://example.com/upload2.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload2.pdf | t
  3 | warrior_workout_3 | https://example.com/upload3.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload3.pdf | t
  4 | warrior_workout_1 | https://example.com/upload1.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload1.pdf | t
  4 | warrior_workout_2 | https://example.com/upload2.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload2.pdf | t
  4 | warrior_workout_3 | https://example.com/upload3.pdf | 2018-02-12 22:25:18.142185+00 | 2018-02-12 22:25:18.142185+00 | upload3.pdf | t
(12 rows)

所以你需要:

with i ( name, url, created_at, updated_at,  file_name, is_main ) as
(VALUES
    (  'warrior_workout_1','https://example.com/upload1.pdf', now(), now(), 'upload1.pdf', TRUE ),
    (  'warrior_workout_2','https://example.com/upload2.pdf', now(), now(), 'upload2.pdf', TRUE ),
    (  'warrior_workout_3','https://example.com/upload3.pdf', now(), now(), 'upload3.pdf', TRUE ))
, s as (
SELECT orders.id
FROM orders
LEFT JOIN uploads
ON uploads.order = orders.id
WHERE uploads.order IS NULL
AND orders.product_type = 'warrior_workout'
)
insert into uploads
select id,i.* 
from i
join s on true
order by id,name;

答案 1 :(得分:1)

您可以使用INSERT INTO SELECTWHERE col1 IN使其正常运作。

INSERT INTO uploads ("order", name, url, created_at, updated_at,  file_name, is_main )    
SELECT filtered_orders.id, 'warrior_workout_1','https://example.com/upload1.pdf', now(), now(), 'upload1.pdf', TRUE    
FROM orders as filtered_orders    
WHERE filtered_orders.id IN (    
   SELECT orders.id    
   FROM orders    
   LEFT JOIN uploads    
   ON uploads.order = orders.id    
   WHERE uploads.order IS NULL  
   AND orders.product_type = 'warrior_workout'  
)

对于您要插入的其他条目,再运行两次查询。

从这里得到启发答案:https://stackoverflow.com/a/22528220/6242649