3

I am trying to insert a number of rows to a table where values came from a select query

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

this should be the same as executing this

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)

and so on.

I saw this How to use a SQL for loop to insert rows into database? but I'm not sure how can this help me

Thanks in advance,

1 Answer 1

4

Use this query:

INSERT INTO article_rel (x_id, y_id)
  SELECT 12, id
  FROM table_name
  WHERE name = 'string'
Sign up to request clarification or add additional context in comments.

Comments

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.