3

我有两张桌子:photos (id,path)tags(id,name)。表是多对多的关系,所以我有第三个表: photos_tags(photos_id, tags_id).

现在,如何将指定路径的照片与指定名称的标签连接起来?我想做这样的事情:

INSERT INTO photos_tags
  SELECT photos.id, tags.id FROM photos, tags
  WHERE photos.path = '/some/path' AND tags.name = 'tag';
4

1 回答 1

2
insert into photos_tags
(photos_id, tags_id)
select id,
    (
        select id
        from Tags
        where name = 'tag'
        )
from photos
where path = '/some/path'
于 2012-04-27T16:00:09.507 回答