nme
nme

Reputation: 685

Creating a connection between to tables in many-to-many relationship

I have two tables: photos (id,path) and tags(id,name). Tables are in many-to-many relationship, so I've got a third table: photos_tags(photos_id, tags_id).

Now, how can I connect a photo of specified path with tag of specified name? I'd 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';

Upvotes: 3

Views: 189

Answers (1)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171371

insert into photos_tags
(photos_id, tags_id)
select id,
    (
        select id
        from Tags
        where name = 'tag'
        )
from photos
where path = '/some/path'

Upvotes: 2

Related Questions