jimmytiler
jimmytiler

Reputation: 83

How to query soundex() in mysql

What is the proper structure for searching within MySql using soundex()? I know how to produce a soundex():

select soundex('str');

just not sure how to include this in my query.

Upvotes: 7

Views: 17315

Answers (4)

Aaron Morefield
Aaron Morefield

Reputation: 995

MySQL has a native way of doing this now!

You can use the syntax SOUNDS LIKE to do a similarity search.

SELECT * FROM table Where SOUNDEX(`field1`) = SOUNDEX(`field2`);

Is equivalent to:

SELECT * FROM table Where `field1` SOUNDS LIKE `field2`;

Upvotes: 2

Luke Madhanga
Luke Madhanga

Reputation: 7457

Obviously, soundex isn't designed for partials like this (e.g. SELECT SOUNDEX('house cleaning'), SOUNDEX('house'), which would not match), but if you would like to perform a nasty SOUNDEX LIKE, you could

SELECT * FROM tablename WHERE SOUNDEX(column) LIKE CONCAT(SOUNDEX('partial_string'), '%')

You could also do

SELECT * FROM tablename WHERE SOUNDEX(column) LIKE CONCAT(TRIM(TRAILING '0' FROM SOUNDEX('hows ')), '%')

This "kinda" works

Upvotes: 3

pala_
pala_

Reputation: 9010

If you're searching for "lewis" against the name field of people table, you perform this query:

SELECT *
FROM people
WHERE soundex("lewis") = soundex(name);

example here

Upvotes: 8

thedarkgriffen
thedarkgriffen

Reputation: 414

This should work

select * from table_name where soundex(column_name) = soundex('word');

This is a good place to read about these:http://www.postgresonline.com/journal/archives/158-Where-is-soundex-and-other-warm-and-fuzzy-string-things.html

Upvotes: 1

Related Questions