HiI am trying to implement a simple tag search mechanism for photos in my database. Photos can be given multiple tags.Here's a summary of what I'm doing so far. There are three tables; one for the photos, one for all the individual tags in the system and one to match tags against photos.create table photo (id bigint, path varchar(100))create table tags (id bigint, tag varchar(25))create table phototags( id bigint, photoid bigint, tagid bigint)
In the stored procedure to search for photos with tags I put all the tag ids (from the tags in the search string) into a temporary table. Then I join the photo table onto the phototags table where the phototags.tagid is IN the temporary table.This gets back all photos containing ANY of the tags in the search string.This isn't what I want. I want the select statement to only find photos which are tagged against ALL the tags in the search string.Here's what the stored procedure does :create table #tagids( id bigint)-- note: I've left out the code which-- populates this table, but it contains-- all tag ids from the search stringselect distinct photo.id, photo.pathfrom photojoin phototags on phototags.photoid = photo.idwhere phototags.tagid in (select id from #tagids)
To illustrate the problem, if I have photo id path -- ---- 1 squirrel.jpg 2 birds feeding.jpgtags id tag -- --- 1 wet squirrel 2 seeds 3 bird table 4 birds phototags id photoid tagid 1 1 1 2 1 2 3 2 2 4 2 3 4 2 4
Squirrel.jpg has the following tags "wet squirrel" seedsbirds feeding.jpg has the following tags seeds "bird table" birds A search for ("wet squirrel" seeds) will bring back one picture, id 1. CorrectlyA search for ("bird table" birds) will bring back 1 picture, id 2. CorrectlyA search for (birds seeds) will bring back both pictures, correctlyHowever, a search for (seeds "bird table" birds) will bring back both pictures because each of these photos contains at least one of the given tags. I actually want it to to return only "birds feeding.jpg", since it is the only image in the database that contains ALL the tags in the search stringHave I described this ok? Can anyone help?Thanks