Having a table T1 contains
TITLE |
---|
age 5 alton john live |
show must go on |
Having a table T2 contains
NAME. | DESCRIPTION |
---|---|
John Bo | alton for kids |
Alton | show age kids |
I would like to split TITLE (T1) and check if at list one token exists in DESCRIPTION (T2) and return TITLE and DESCRIPTION
Expected Output:
TITLE | DESCRIPTION |
---|---|
age 5 alton john live. | alton for kids |
age 5 alton john live. | show age kids |
show must go on | show age kids |
2
Answers
The following SQL query can be used to split the titles in
T1
and check if at least one token exists in the descriptions inT2
:The query uses a combination of the
JOIN
andLIKE
operators to find the matching records between the tablesT1
andT2
.Main problem here is to wrangle table t1 to associate each "TITLE" value to each sequence of characters separated by space. Then you can match your wrangled table with your t2 table on a regex pattern. May require quite a bit of time as doing a join on such string condition is not very efficient, but it works.
You can generate that association with a recursive query, that exploits
SUBSTRING_INDEX
to retrieve last word of "TITLE" and a numerical index that is decreased. Recursion would stop once that numerical index reaches 0.Check the demo here.
Assumption: your sentences have no punctuation, and every word is separated by a space character.