MySQL query to search a string ‘demo’ if someone mistakenly types ‘deom’?


For this, you can use SOUND along with the LIKE operator. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.95 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values('Adam');
Query OK, 1 row affected (0.33 sec)
mysql> insert into DemoTable values('Johm');
Query OK, 1 row affected (0.29 sec)
mysql> insert into DemoTable values('Carol');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('SAMSUNG');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('SAMSUMG');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------+
| Name    |
+---------+
| John    |
| Adam    |
| Johm    |
| Carol   |
| SAMSUNG |
| SAMSUMG |
+---------+
6 rows in set (0.00 sec)

Here is the query to search a string which is inserted in the table as a typo, for example, “deom” for “demo”, ‘samsumg” for “samsung” −

mysql> select *from DemoTable where Name SOUNDS LIKE 'samsung';

This will produce the following output −

+---------+
| Name    |
+---------+
| SAMSUNG |
| SAMSUMG |
+---------+
2 rows in set (0.00 sec)

Updated on: 13-Dec-2019

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements