Apologies if the title is unclear, I'll try to explain it with an example. Let's say I have a file tagged with "trees" and "greenery" and I want to search for it, however when I enter the terms "trees" and "greenery" into the input field it gives me results that are tagged as either one or both of them.
Here is my database structure:
For the query I'm using the IN operator, and it looks something like this:
SELECT path FROM tagged_files INNER JOIN file_tags ON tagged_files.tag_id=file_tags.id INNER JOIN files ON tagged_files.file_id = files.id WHERE file_tags.tag IN ($inQuery) GROUP BY files.id ORDER BY files.id;
The $inQuery
variable is defined as: $inQuery = implode(',', array_fill(0, count($tags), '?'));
and $tags
is a string from the search query, I later bind the values.
How do I get the query to select only files that match ALL of the tags I entered?