I am currently working on a system to allow documents to be tagged with keywords and have a table of the form:
CREATE TABLE `KeywordsToDocuments` (
`keywordID` int NOT NULL,
`documentID` int NOT NULL);
It is possible for each document to be associated with many keywords, and for each keyword to be associated with many documents. Unfortunately a simple AND
statement does not achieve what I need as no single row will satisfy multiple values of KeywordID, instead a publication is associated with several keywords by inclusion of its ID on several rows with different keywordIDs.
I am attempting to write a query which will give me a list of documents that are tagged with all of an arbitrary number of keywords. My initial attempt was the following which uses the INTERSECT
statement:
SELECT documentID FROM KeywordsToDocuments WHERE KeywordID=keyword1
INTERSECT
SELECT documentID FROM KeywordsToDocuments WHERE KeywordID=keyword2
...
Where the ...
can be an arbitrary number of similar statements for further keywords. This query is built by a PHP script dynamically.
What I am trying to do here is to find all of the documents that have an association with each keyword individually, then from those result sets find the intersect which will be documents which are associated with all off the requested keywords.
However I just learned that MySQL does not support the INTERSECT
statement. I have been looking for alternatives but all the resources I have found on replacing INTERSECT
have focussed on it being used to merge results from two different tables. I cannot see a way in this case to convert to another statement such as an INNER JOIN
.
How can make a query like this work without using the INTERSECT
statement to permit compatibility with MySQL?