How to query soundex() in mysql
Asked Answered
D

4

6

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.

Dermatogen answered 13/4, 2015 at 4:52 Comment(3)
where soundex("search_string") = soundex(search_column)Prendergast
You can see an example here #24250767Religiose
I have tried your suggestion pala_ with no luck; it returns no results. I have been searching with the soundex() code, for example, the soundex() code for 'Lewis' is soundex(L200). I'm assuming I search with the soundex() code rather than text or else this defeats the purpose of having a code for soundex() in the first place right.Dermatogen
P
7

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

Prendergast answered 13/4, 2015 at 5:17 Comment(3)
That worked, thanks. But why is there a soundex() code then, e.g. SELECT SOUNDEX('Lewis')?Dermatogen
it's generating the codes for both the search term, AND the fields it's comparing against. eg soundex("lewis") = L200 and soundex("luis") = L200. So soundex("lewis") = soundex("luis") matches, whereas "lewis" = "luis" obviously would not. Words that "sound the same" in english should usually have the same code.Prendergast
Ahh, thanks so much; I've been trying to figure this out for a long time now.Dermatogen
C
3

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

Christlike answered 9/8, 2019 at 11:15 Comment(0)
C
2

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`;
Calvin answered 3/11, 2021 at 22:33 Comment(0)
C
1

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

Citrin answered 13/1, 2017 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.