I have a table structure which is basically:
Items
ID: INT(10)
Title: CHAR(255)
ItemsWords
ID: INT(10)
ItemID: INT(10)
Word: CHAR(50)
For each ‘Item’ record, there are multiple records in the ‘ItemsWords’ table.
I can find all items using this which contain a single word like this:
SELECT * FROM Items i, ItemsWords w WHERE w.ItemID=i.ID AND w.Word='WORD_TO_FIND'
I can find all items using this which contain any of a number of words like this:
SELECT * FROM Items i, ItemsWords w
WHERE w.ItemID=i.ID
AND (w.Word='WORD_TO_FIND' OR w.Word='ANOTHER_WORD_TO_FIND')
But I also want to be able to use the same table structure to find only the ‘Items’ which contain ALL of any number of search words.
I know I could use FullText by the Items table has over 100,000 records and the ItemWords table many time more than that, so have set up the structure this way to make these searches very quick and with minimum server impact. The one word or ANY word searches above are indeed super speedy.
Thank you!
The number in the final line is the number of words in the list of words you are searching for (ie: 2 in this instance.
Note how the tables are joined using the
inner joinsyntax, rather than awhereclause