I'm making hashtag system for my webpage, and have three tables:
- items - ID and some others
- tags - ID and name
- item_tags - junction table with IDitem and IDtag
Selecting all items with given hashtag is pretty easy:
SELECT items.* FROM items
join item_tags on items.ID=item_tags.IDitem
join tags on item_tags.IDtag=tags.ID
where tags.name="something";
Question is, what should I do if I want to select all items with multiple tags, for example, find all items tagged as cat and animal?
I've thought about making temporary table, inserting all items with first tag, then leaving those with second tag, then third, then fourth and so on, but it doesn't look too good and too fast.
Just find all that match both tags, using
IN
. Like this:well you know your list, so that is a simple string. and you know your count. these can be jammed into a mysql
Prepared Statement
and executed.But below it is with the list and count plopped in just for demo purposes.