I have two MySQL tables:
- product has id, title, price and some other product attributes
- tag has *product_id* and *tag_name* fields
each product row can have one ore more rows in table tag joined by *product_id* field.
Question: how do I select products by *tag_name* and other attributes?
Example: I need to select all books cheaper than $10 and tagged ‘foreign’ and ‘sci-fi’
I know how to select products with one tag (join product and tag by *product_id* and set where clause)
I know how to select products with more than one tag using a stored procedure, however queries take very long time to run.
Is there a way to avoid stored procedure and still get the result?
Update: StackOverflow does something like this when you click “Related Tags” on the right hand side bar. Now, I don’t know what architechture their DB has, but the result is fairly quick selection of all questions tagged with multiple tags.
This will return any product with price less than 10, and with tag foreign or sci-fi
How about something like
This will ensure that it has both tags, assuming that tags has primary key product_id and tag_name