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
160 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 41

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 41

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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql ide 10 39
sql server computed columns 11 29
Display images from mysql blob type (Not working) 9 26
Connection to multiple databases 13 16
Read about achieving the basic levels of HRIS security in the workplace.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…

785 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