简体   繁体   中英

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

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; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM