I have two tables.
-
Table
A
:Company_name ------------------------------- Tata consultancy Infosys tech Atm service Air force firm
-
Table
B
:Company1_name | Id -------------------------------- ----- Atm and service. | 1 Honey tech. | 2 Tata & consultancy. | 3 Graft soft. | 4 Atm, service. | 5
I want this result:
Id
----
1
3
5
Description:
For each
Company_name
in tableA
, find a row in tableB
whosecompany1_name
value has all the words from tableA
's value, and return the matching row'sId
.
How to design a query for this in MySQL?
Best Answer
For a quick and dirty solution - not 100% accurate due to some full-text search specifics but will get most of the matches - add a
FULLTEXT
key toA
and then use a join with be withMATCH AGAINST
as the join condition. SinceMATCH AGAINST
cannot use a a non-constant argument, you will have to simulate the join with a cursor in a stored procedure. Below is a fully-functional tested example:More info on full text keys in the MySQL manual at https://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
This is suitable for relatively small tables and in circumstances where high latency is acceptable. For better performance on large datasets and perfect accuracy, you will need to implement some form of external full text indexing.