Tuesday 30 July 2019

How the SOUNDS LIKE Operator Works in MySQL

In MySQL, you can use the SOUNDS LIKE operator to return results that sound like a given word.
This operator works best on strings in the English language (using it with other languages may return unreliable results).

Syntax

The syntax goes like this:
expr1 SOUNDS LIKE expr2
Where expr1 and expr2 are the input strings being compared.
This operator is the equivalent of doing the following:
SOUNDEX(expr1) = SOUNDEX(expr2)

Example 1 – Basic Usage

Here’s an example of how to use this operator in a SELECT statement:
SELECT 'Damn' SOUNDS LIKE 'Dam';
Result:
+--------------------------+
| 'Damn' SOUNDS LIKE 'Dam' |
+--------------------------+
|                        1 |
+--------------------------+
In this case, the return value is 1 which means that the two input strings sound alike.
Here’s what happens if the input strings don’t sound alike:
SELECT 'Damn' SOUNDS LIKE 'Cat';
Result:
+--------------------------+
| 'Damn' SOUNDS LIKE 'Cat' |
+--------------------------+
|                        0 |
+--------------------------+

Example 2 – Compared to SOUNDEX()

Here it is compared to SOUNDEX():
SELECT 
  'Damn' SOUNDS LIKE 'Dam' AS 'SOUNDS LIKE',
  SOUNDEX('Dam') = SOUNDEX('Damn') AS 'SOUNDEX()';
Result:
+-------------+-----------+
| SOUNDS LIKE | SOUNDEX() |
+-------------+-----------+
|           1 |         1 |
+-------------+-----------+

Example 3 – A Database Example

Here’s an example of how we can use this operator within a database query:
SELECT ArtistName FROM Artists
WHERE ArtistName SOUNDS LIKE 'Ay See Dee Ci';
Result:
+------------+
| ArtistName |
+------------+
| AC/DC      |
+------------+
And here it is using SOUNDEX():
SELECT ArtistName FROM Artists
WHERE SOUNDEX(ArtistName) = SOUNDEX('Ay See Dee Ci');
Result:
+------------+
| ArtistName |
+------------+
| AC/DC      |
+------------+

0 comments:

Post a Comment