Creating a table-to-many relationship between tables

I have two tables: photos (id,path)and tags(id,name). Tables are, in many ways, so I have a third table: photos_tags(photos_id, tags_id).

Now, how can I associate a photo of a specified path with a tag of a specified name? I would like to do something like this:

INSERT INTO photos_tags
  SELECT photos.id, tags.id FROM photos, tags
  WHERE photos.path = '/some/path' AND tags.name = 'tag';
+3
source share
1 answer
insert into photos_tags
(photos_id, tags_id)
select id,
    (
        select id
        from Tags
        where name = 'tag'
        )
from photos
where path = '/some/path'
+2
source

All Articles