I have a large insert query like this:
INSERT INTO video_tags
(vid_id, tag_id)
VALUES
(1,7),
(2,46),
(2,52) Etc. Etc.
However I don't have the tag_ids to insert, only the tag string. Therefore I need to lookup the tag id as I am inserting them. The tags are stored in a lookup table with two columns:
+-----+---------+
| id | int |
| tag | varchar |
+-----+---------+
I want to do something like this:
INSERT INTO video_tags
(vid_id, tag_id)
VALUES
(1, SELECT id FROM tags WHERE tag = 'banana'),
(2, SELECT id FROM tags WHERE tag = 'tree')
(2, SELECT id FROM tags WHERE tag = 'chimps')
...but this doesn't work and anyway looks cumbersome and inefficient.
I have also seen this done:
INSERT INTO video_tags
(vid_id, tag_id)
VALUES
SELECT '1', id FROM tags WHERE tag = 'banana')
...but I can't get this to work with multiple selects.
vid_ids and tag strings, then doing something likeinsert into...from (select vid_id,tag_idvid_ids and tag strings. It hasvid_ids andtag_ids.