I have no idea if this is possible to do in one SQL statement. I have been reading all day and can't seem to find a solution and to be frank, my experience is only at the tip of the iceberg when it comes to SQL.
I have two different tables, lets say: items and customers. Both tables have a field (Notes) of type TEXT containing words separated with spaces and CR/LF pairs.
I need to select all rows (items.*) where any of the words in customers.note matches any word in items.note
Example:
Table items
ID NOTES
-- ----------
1 bag candle fork
2 max kielland
3 plate knife fork
4 wheel brownbag
Table customers
ID NOTES
-- ----------
1 brown candle fork
The result I want would be:
Items.ID Items.Note
-------- ----------
1 bag candle fork
3 plate knife fork
I have been looking at IN and LIKE (as separate solutions, not together as a pair) but they don't seem to quite solve the problem. If it is to any help, I can make sure all spaces are replaced by ','
In the long end it would be even more perfect if the compare could be made as the LIKE %..% producing a result like this:
Items.ID Items.Note
-------- ----------
1 bag candle fork
3 plate knife fork
4 wheel brownbag
as you can see the brown get a hit on brownbag.
As I said I don't know if this is possible in MySQL without splitting up the query in several questions with some PHP processing.
Thank you.
Update
With the help of OMG Ponies MATCH..AGAINST suggestion and some PHP I solved it.
$SQLSet = mysql_query("
SELECT DISTINCT i.*
FROM items i
JOIN wanted w ON MATCH(i.notes)
AGAINST(\"".$SQLRowWanted['Notes']." +'".$SQLRowWanted['Name']."'\")
");
This is running inside a loop with $SQLRowWanted from an earlier call. By encapsulating the Name column in '...' and adding a + before it, I get a higher match rate on the full name as well.