TheDarkVoid TheDarkVoid - 25 days ago 18
SQL Question

SQL: Using data from one table to insert new data into another table

I am trying to write a query to create a row in a table "tagmap" which will use a constant value "tagid" and the "id" of a row in table images. I want this to be done for each row in the other table.

I've tried quite a few things but essentially this is what I want to end up with:

INSERT INTO tagmap SELECT images.id FROM images (id, tagid, imageid) VALUES(tagid || images.id, tagid, images.id);

dmg dmg
Answer

All you have to do is project the values you want to insert. No need for "values" when you are using select:

INSERT INTO tagmap(id, tagid, imageid) 
  SELECT 'tagid' || images.id, 'tagid', images.id FROM images; 
Comments