How to query soundex() in MySQL?

MySQLMySQLi Database

The SOUNDEX() returns a soundex string. Two strings that sound almost the same should have identical soundex strings

To query soundex() in MySQL, you can use the below syntax −

select *from yourTableName where soundex(yourValue)=soundex(yourColumnName);

Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(20),
   LastName varchar(20)
   );
Query OK, 0 rows affected (0.28 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(FirstName,LastName) values('Adam','Smith');
Query OK, 1 row affected (0.04 sec)

mysql> insert into DemoTable(FirstName,LastName) values('David','Miller');
Query OK, 1 row affected (0.07 sec)

mysql> insert into DemoTable(FirstName,LastName) values('Carol','Taylor');
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(FirstName,LastName) values('Chris','Brown');
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(FirstName,LastName) values('John','Smith');
Query OK, 1 row affected (0.07 sec)

Display all records from the table using select statement

mysql> select *from DemoTable;

This will produce the following output −

+----+-----------+----------+
| Id | FirstName | LastName |
+----+-----------+----------+
| 1  | Adam      | Smith    |
| 2  | David     | Miller   |
| 3  | Carol     | Taylor   |
| 4  | Chris     | Brown    |
| 5  | John      | Smith    |
+----+-----------+----------+
5 rows in set (0.00 sec)

Following is the query for soundex() in MySQL.

mysql> select *from DemoTable where soundex('Smith')=soundex(LastName);

This will produce the following output −

+----+-----------+----------+
| Id | FirstName | LastName |
+----+-----------+----------+
| 1  | Adam      | Smith    |
| 5  | John      | Smith    |
+----+-----------+----------+
2 rows in set (0.00 sec)
raja
Updated on 30-Jul-2019 22:30:26

Advertisements