0

Looking to insert 500 rows into many-to-many table (sample_tag).

Aiming to have 500 new records created in sample_tag linking the first 500 rows from sample to associated tag_id in the tag table.

The following code seems to make sense, but triggers an error because the nested SELECT statement returns more than one row:

INSERT INTO sample_tag (sample_id, tag_id) 
VALUES ((SELECT sample_id FROM sample WHERE sample_id <= 500), 1)

What could be the correct SQL to accomplish this insert for multiple rows?

1 Answer 1

1

You can have multiple inserts by not using VALUES keyword. You need to specify same number of columns on your source table.

INSERT INTO sample_tag (sample_id, tag_id) 
SELECT sample_id, tag_id from sample where sample_id<=500
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!!! Works like a charm. "tag_id" is in a separate table, but was able to modify the code to INSERT INTO sample_tag (sample_id, tag_id) SELECT sample_id, "1" from sample where sample_id<=500

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.