How to check similarity between two strings in MySQL?

MySQLMySQLi Database

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)
raja
Published on 20-Nov-2018 07:27:41
Advertisements