Suppose I have a table that list some items with a column title
Now given an item with title "AAA BBB CCC DDD" I would like to retrieve all items that has at least n-1
matching words.
At the moment I'm producing all combination (php side) of n-1
words and then use a LIKE to do the match, so something like this:
SELECT
`e`.`title`
FROM `items` AS `e`
WHERE ((
(e.title LIKE '%AAA%' AND e.title LIKE '%BBB%' AND e.title LIKE '%CCC%') OR
(e.title LIKE '%AAA%' AND e.title LIKE '%BBB%' AND e.title LIKE '%DDD%') OR
(e.title LIKE '%AAA%' AND e.title LIKE '%CCC%' AND e.title LIKE '%DDD%') OR
(e.title LIKE '%BBB%' AND e.title LIKE '%CCC%' AND e.title LIKE '%DDD%')))
Questions
I'm far from being an SQL guru :) Is there a better approach that the above example ?
An extra requirement would be to search for items with
n-2
words in case no item withn-1
is found. My idea is to find all matching items withn-1
,n-2
until 2 and in the result set have a dynamic column containing the number of matched words and so order the result on the base of this score, is it possible ?
extra info:
I cannot alter the table structure/property. ( I will break the system "consistency" as I'm on Magento so for "Item" I mean products)
Title is actually on a separate table so in the real scenario it is on a joined table