0

I have a query that returns about 1k results. A single column of 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'

I also have this INSERT query:

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 )

I need to perform the insert query for each returned ID from the first query. How can I combine these to get the desired result?

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
DigitalDesignDj
  • 1,725
  • 15
  • 16

2 Answers2

1

here is comprehensive example:

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)

so you need to:

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;
Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
1

You can use INSERT INTO SELECT and WHERE col1 IN to make it work.

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'  
)

Run the query two more times for other entries you want to insert.

Answer inspired from here: https://stackoverflow.com/a/22528220/6242649

Udayraj Deshmukh
  • 1,814
  • 1
  • 14
  • 22