How to check similarity between two strings in MySQL?


Similarity between two strings can be checked with the help of ‘strcmp()’ function. Here are the conditions.

  • If both strings are equal, then it returns 0.

  • If first string is less than the second string, it returns -1.

  • If first string is greater than the second string, it returns 1.

Here is an example.

Case 1 − If both strings are equal.

The following is the query.

mysql > SELECT STRCMP("demo", "demo");

The following is the output of the above query.

+------------------------+
| STRCMP("demo", "demo") |
+------------------------+
| 0                      |
+------------------------+
1 row in set (0.00 sec)

Case 2 − If first string is less than the second string.

The following is the query.

mysql> SELECT STRCMP("demo", "demo1234");

The following is the output of the above query.

+----------------------------+
| STRCMP("demo", "demo1234") |
+----------------------------+
| -1                         |
+----------------------------+
1 row in set (0.00 sec)

Case 3 − If first string is greater than the second string.

The following is the query.

mysql> SELECT STRCMP("demo1", "demo");

The following is the output.

+-------------------------+
| STRCMP("demo1", "demo") |
+-------------------------+
| 1                       |
+-------------------------+
1 row in set (0.00 sec)

Updated on: 26-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements