Development

The MySQL soundex function; Selecting names

Soundex is the algorithm for indexing names by sound or as pronounced. The MySQL SOUNDEX() function returns a Soundex string from a string supplied.

It can also be used to find same-sounding strings from a table.

The example people table:

| id | first_name |
|----|------------|
|  1 |      Aiden |
|  2 |      Aidan |
|  3 |       Ryan |
|  4 |        Tim |
|  5 |      Ayden |
|  6 |       Matt |
|  7 |      Aydin |

You can see there are several “aidens” but they are spelt different however essentially pronounced the same.

This is where SOUNDEX() can help because using a LIKE query will not.

An example SOUNDEX() query to get all names that sound like “aiden”

SELECT `id`, `first_name` FROM `people` WHERE soundex("aiden") = soundex(first_name);

The result:

| id | first_name |
|----|------------|
|  1 |      Aiden |
|  2 |      Aidan |
|  5 |      Ayden |
|  7 |      Aydin |

SOUNDEX() does its job in finding the values that sound the same as “aiden”.

SELECT `id`, `first_name` FROM `people` WHERE soundex("aidn") = soundex(first_name);

Returns the same results, however using soundex("aid") returns nothing, because it doesn’t sound like Aiden.

Mysql Docs for SOUNDEX()

SQL Fiddle example

Share
Tags: MYSQLWeb Dev

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago