Hi all,
I have 3 tables:
File (FileID,name)
Tags (TagId,Tag)
files_tag(FileID,TagID)
I'd like to be able to search File based on keyword tags. files_tag stores a relationship between the two tables. I'd like to return files if all keywords are matched.
So.. if I have two files in File: (1,'a masterpiece')(2,'photograph')
and two tags in Tags (1,'photography')(2,'illustration')
And files_tag relationship are as follows: (1,1)(1,2)(2,1)
(so 'masterpiece' has two tags and 'photograph' has just one)
What would the query be to search for a file which contains both 'photo' and 'illustration', ie: return 'a masterpiece' only. ( but if i had searched for just 'photo' both files would have been returned)
This is my SQL so far which isn't working properly (I'm sure the logic is off, but I'm not sure of the fix)
SELECT
f.name,
t.Tag
FROM files_tag_r AS ftr
LEFT JOIN files AS f
ON ftr.FileID = f.FileID
LEFT JOIN tags AS t
ON ftr.TagID = t.TagID
WHERE
t.Tag LIKE '%illustration%'
AND t.Tag LIKE '%photo%'
ORDER BY f.name
Obviously if there is a different way to store the data then I'm open to suggestions!
Thanks in advance.