I am implementing a tag system for a website. For each object there are several tags and several objects for each tag. This is achieved by storing a table with two values for each record, one for the object identifiers and the tag.
I want to write a query to find the objects matching the specified set of tags. Suppose I had the following data (in the format [object] → [tags] *)
apple -> fruit red food
banana -> fruit yellow food
cheese -> yellow food
firetruck -> vehicle red
If I want to match (red), I have to get an apple and shoot. If I want to match (fruit, food), I have to get (apple, banana).
How to write a SQL query to do what I want?
@Jeremy Ruten,
Thanks for your reply. The notation used was used to provide some sample data. There is a table in my database with 1 object identifier and 1 tag per entry.
Secondly, my problem is that I need to get all the objects matching all the tags. Substituting your OR for AND like this:
SELECT object WHERE tag = 'fruit' AND tag = 'food';
The result does not work at startup.
source
share