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


INSERT INTO uploads ( id, order, name, url, file_name, is_main ) VALUES
  (DEFAULT, orders.customer, "warrior_workout_nonhomeworkout","https://example.org","6 Week Warrior Workout Non-Home Workout.pdf",true),
  (DEFAULT, orders.customer, "warrior_workout_homeworkout","https://example.org","6 Week Warrior Workout Home Workout.pdf",true),
  (DEFAULT, orders.customer, "men_six_week_summer_shredder","https://example.org","Men's Six Week Summer Shredder.pdf",true);

I have 2 postgres queries, and I need to combine them into one.

3
  • 1
    Can you explain a bit more? You're showing us two queries, but one is an straightforward insert, the other is a select -- you probably don't want to combine those two. Can you show us what queries you have, what you've tried, and what you want the output to look like? Commented Feb 12, 2018 at 20:08
  • This is it. I want to do 3 INSERT per record returned by the first query. Commented Feb 12, 2018 at 20:10
  • I actually made a mistake in my example. One of the rows to populate is named "order". I made an edit, but it just made it works. Commented Feb 12, 2018 at 23:06

1 Answer 1

2

I think you want this:

INSERT INTO uploads (customer, name, url, file_name, is_main ) 
    SELECT o.customer, v.*
    FROM orders o LEFT JOIN
         uploads u
         ON u.order = o.id CROSS JOIN
         (VALUES ('warrior_workout_nonhomeworkout', 'https://example.org', '6 Week Warrior Workout Non-Home Workout.pdf', true),
                 ('warrior_workout_homeworkout', 'https://example.org', '6 Week Warrior Workout Home Workout.pdf', true),
                 ('men_six_week_summer_shredder', 'https://example.org', 'Men''s Six Week Summer Shredder.pdf', true)
         ) v(name, url, file_name, is_main)
    WHERE u.order IS NULL AND o.product_type = 'warrior_workout';

For each of the customers missing the upload, this inserts those three values.

Sign up to request clarification or add additional context in comments.

1 Comment

Gordon is using the VALUES statement to create a fake table called v. v.* just means "all the columns in v".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.