在MySQL中安裝Levenshtein函數
問題:
問題:CREATE FUNCTION levenshtein(s1 VARCHAR(255), s2 VARCHAR(255)) RETURNS INT BEGIN DECLARE len1 INT; DECLARE len2 INT; DECLARE i INT; DECLARE j INT; DECLARE c INT; DECLARE cost INT; DECLARE d INT; DECLARE tmp INT; SET len1 = LENGTH(s1); SET len2 = LENGTH(s2); DECLARE matrix[len1 + 1][len2 + 1] INT; FOR i = 0 TO len1 DO SET matrix[i][0] = i; END FOR; FOR j = 0 TO len2 DO SET matrix[0][j] = j; END FOR; FOR i = 1 TO len1 DO FOR j = 1 TO len2 DO IF s1[i] = s2[j] THEN SET cost = 0; ELSE SET cost = 1; END IF; SET d = matrix[i - 1][j] + 1; SET c = matrix[i][j - 1] + 1; SET tmp = matrix[i - 1][j - 1] + cost; IF d < c THEN IF d < tmp THEN SET matrix[i][j] = d; ELSE SET matrix[i][j] = tmp; END IF; ELSE IF c < tmp THEN SET matrix[i][j] = c; ELSE SET matrix[i][j] = tmp; END IF; END IF; END FOR; END FOR; RETURN matrix[len1][len2]; END
要將Levenshtein 函數
要將Levenshtein 函數$query = "SELECT levenshtein('abcde', 'abced')"; $result = mysqli_query($link, $query); $row = mysqli_fetch_array($result); echo $row['levenshtein(abcde, abced)']; // Output: 2要將Levenshtein 函數要將Levenshtein 函數要將Levenshtein 函數請依照以下步驟使用MySQL Workbench:連接到您的MySQL server.執行以下語句:使用示例加入Levenshtein函數後就可以使用了在PHP中如下:
以上是如何將 Levenshtein 函數整合到 MySQL 中以用於 PHP 搜尋應用程式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!