Table of Contents
How to Calculate String Similarity in MySQL
Implementation using MySQL Functions
Calculating Similarity Percentage
Home Database Mysql Tutorial How to Efficiently Calculate String Similarity Percentage in MySQL?

How to Efficiently Calculate String Similarity Percentage in MySQL?

Dec 03, 2024 pm 09:49 PM

How to Efficiently Calculate String Similarity Percentage in MySQL?

How to Calculate String Similarity in MySQL

To determine the similarity between two strings stored in MySQL, consider using an efficient algorithm like the Levenshtein distance. This approach calculates the number of single-character edits (additions, removals, or substitutions) required to transform one string into another.

Implementation using MySQL Functions

MySQL provides a series of functions for string manipulation. Here's an example function (levenshtein) that computes the Levenshtein distance between two strings, s1 and s2:

CREATE FUNCTION `levenshtein`(s1 text, s2 text) RETURNS int(11)
DETERMINISTIC
BEGIN
  # Initialize variables
  DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
  DECLARE s1_char CHAR;
  DECLARE cv0, cv1 text;
  SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2),
    cv1 = 0x00, j = 1, i = 1, c = 0;

  # Check for edge cases
  IF s1 = s2 THEN
    RETURN 0;
  ELSEIF s1_len = 0 THEN
    RETURN s2_len;
  ELSEIF s2_len = 0 THEN
    RETURN s1_len;
  ELSE
    # Main loop over s1 characters
    WHILE j <= s2_len DO
      SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
    END WHILE;

    WHILE i <= s1_len DO
      SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)),
        j = 1;

      # Inner loop over s2 characters
      WHILE j <= s2_len DO
        SET c = c + 1;
        IF s1_char = SUBSTRING(s2, j, 1) THEN
          SET cost = 0;
        ELSE
          SET cost = 1;
        END IF;

        SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
        IF c > c_temp THEN SET c = c_temp; END IF;
        SET c_temp = CONV(HEX(SUBSTRING(cv1, j + 1, 1)), 16, 10) + 1;
        IF c > c_temp THEN SET c = c_temp; END IF;
        SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
      END WHILE;

      SET cv1 = cv0, i = i + 1;
    END WHILE;
  END IF;

  # Return Levenshtein distance
  RETURN c;
END
Copy after login

Calculating Similarity Percentage

With the Levenshtein distance computed, you can calculate the similarity percentage as follows:

CREATE FUNCTION `levenshtein_ratio`(s1 text, s2 text) RETURNS int(11)
DETERMINISTIC
BEGIN
  DECLARE s1_len, s2_len, max_len INT;
  SET s1_len = LENGTH(s1), s2_len = LENGTH(s2);
  IF s1_len > s2_len THEN
    SET max_len = s1_len;
  ELSE
    SET max_len = s2_len;
  END IF;
  RETURN ROUND((1 - LEVENSHTEIN(s1, s2) / max_len) * 100);
END
Copy after login

For example, if you have two strings in MySQL:

SET @a = "Welcome to Stack Overflow";
SET @b = "Hello to stack overflow";
Copy after login

Using the levenshtein_ratio function:

SELECT LEVENSHTEIN_RATIO(@a, @b);
Copy after login

Will return a value representing the similarity percentage between the two strings, eg. 60 for the provided example.

The above is the detailed content of How to Efficiently Calculate String Similarity Percentage in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

How do I configure SSL/TLS encryption for MySQL connections?

See all articles