views:

46

answers:

2

I'm quite new to SQL and have a question about matching names from two columns located within a table:

Let's say I want to use the soundex() function to match two columsn. If I use this query:

SELECT * FROM tablename WHERE SOUNDEX(column1)=SOUNDEX(column2);

a row is returned if the two names within that row match. Now I'd also like to get those name matches between column1 and column2 that aren't in the same row. Is there a way to automate a procedure whereby every name from column1 is compared to every name from column2?

Thanks :)

p.s.: If anyone could point me in the direction of a n-gram/bi-gram matching algorithm that is easy for a noob to implement into mysql that would be good as well.

+3  A: 

If your table has a key, say id, you can try:

select A.column1, B.column2 
from tablename as A, tablename as B 
where (A.id != B.id) and (SOUNDEX(A.column1) = SOUNDEX(B.column2))
codaddict
one of my favorites, joining the same table (a few addicts know this)
Pentium10
to support mysql <4.1, should be `A.column1 sounds like B.column2`, SOUNDEX(expr1)=SOUNDEX(expr2) (available only in version 4.1 or later).
Pentium10
Thanks, that works very well :)
MrFancypants
A: 

You can join the table to itself on that relationship as such:

SELECT * FROM tablename t1 JOIN tablename t2 
ON SOUNDEX(t1.column1) = SOUNDEX(t2.column2);
hhunter
This does not exclude entries from same row as OP wants.
codaddict
Thanks, this one is useful for me as well as I don't have to divide my query into two queries if I want all matches.
MrFancypants