Solved

I'm looking for the best way to join two tables in MySql based on the matching of the words in a varchar field in one table against a varchar field on another table

Posted on 2009-06-30
4
161 Views
Last Modified: 2012-05-07
I have two tables with a description column in each one. The first table may have between 500,000 and 1,000,000 records, the second one may have between 1,000 and 10,000 records. I need to get the join of both tables based on the descriptions fields in each table but the problem is I should search the words in the descriptions fields in any order. For example "My house is red" should join with "Red is my house" or with "is red my house". So the words in the description field on the first table should be the same to the words in the description field on the second table without considering the order of those words.
I could create in both tables 7 or 8 varchar fields (the descriptions are never longer than 8 words) to store all the words from the descriptions fields of both tables if it could help.
How can I solve this having a good performance?


In table_b, word_1 has the first word of the description field for each record in this table, word_2 has the second word of the description field for each record in this table, and so on:
 
select a.*, b.*
from table_a a, table_b b
where a.descrip like b.word_1 
            and a.descrip like b.word_2 
            and a.descrip like b.word_3 
            and a.descrip like b.word_4 
            and a.descrip like b.word_5 
            and a.descrip like b.word_6

Open in new window

0
Comment
Question by:egrinblat
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 24750553
You don't need several more columns just one containing description with words sorted alphabetically (and converted to lowercase possibly). Then you may simply compare these new descriptions in join.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24750564
And you should create index on these new columns to ensure good performance.
0
 
LVL 14

Expert Comment

by:shru_0409
ID: 24752386
select *
from table_a a, table_b b
where a.column_name = b.column_name -- ref columns from both table
and a.descrip like '%'|| b.descrip || '%'

try this
0
 

Author Closing Comment

by:egrinblat
ID: 31598558
Thank you very much, that's an easy to implement and excellent idea.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Complex SQL statement in VB.NET 7 31
How to fix Datetime in MySQL? 4 48
Select values in a row based on values in another row in sql 4 26
SQL query and VBA 5 45
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question