0

How can I use soundex function on the following WHERE clause?

WHERE usr.name LIKE CONCAT('%', :search, '%')

Given that the general approach for using SOUNDEX function is:

SELECT name FROM users WHERE soundex('Dennis') = soundex(name)

However, I am getting confused on how to code it on the above where clause as I have used LIKE. I am using MySQL and PDO.

Relaxing Music
  • 452
  • 4
  • 13
  • soundex isn't very useful for real world cases, why do you want to use it? – ysth Sep 01 '21 at 18:38
  • 1
    @ysth If used properly and at correct place, it is. Whether or not how much useful it is, depends upon the area of operation. According to me, it's a really powerful function very few know about. I am using it in my chat application where I have a list of connected users whom people can search using search bar. Not everyone is good in writing correct spellings. Mistakes are made. People typing in hurry also make errors. If the name typed would sound same as the name that was intended to be searched, it will show up the correct result even if the spelling typed was incorrect. – Relaxing Music Sep 01 '21 at 18:41

1 Answers1

1

I got the answer. Posting for someone searching in future.

WHERE (soundex(:search) = soundex(usr.name)) OR (usr.name LIKE CONCAT('%', :search, '%'))
Relaxing Music
  • 452
  • 4
  • 13