Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Porównaj różnice tekstowe między dwoma prawie identycznymi wierszami / tabelami w MySql

Chociaż łatwiej byłoby to zrobić w kodzie aplikacji, jest to możliwe za pomocą kilku funkcji MySQL:

delimiter //

drop function if exists string_splitter //
create function string_splitter(
  str text,
  delim varchar(25),
  pos tinyint) returns text
begin
return replace(substring_index(str, delim, pos), concat(substring_index(str, delim, pos - 1), delim), '');
end //

drop function if exists percentage_of_matches //

create function percentage_of_matches(
  str1 text,
  str2 text)returns double
begin
set str1 = trim(str1);
set str2 = trim(str2);
while instr(str1, '  ') do
  set str1 = replace(str1, '  ', ' ');
end while;
while instr(str2, '  ') do
  set str2 = replace(str2, '  ', ' ');
end while;
set @i = 1;
set @numWords = 1 + length(str1) - length(replace(str1, ' ', ''));
set @numMatches = 0;
while @i <= @numWords do
  set @word = string_splitter(str1, ' ', @i);
  if str2 = @word or str2 like concat(@word, ' %') or str2 like concat('% ', @word) or str2 like concat('% ', @word, ' %') then
    set @numMatches = @numMatches + 1;
  end if;
  set @i = @i + 1;
end while;
return (@numMatches / @numWords) * 100;
end //

delimiter ;

Pierwsza funkcja jest używana w drugiej, czyli tej, którą chcesz wywołać w swoim kodzie, na przykład:

select percentage_of_matches('salt water masala', 'salt masala water');
select percentage_of_matches('water onion maggi milk', 'water onion maggi');



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Jak włączyć INNODB w mysql

  2. kod java do importowania danych xls do bazy mysql

  3. Funkcja wyszukiwania z greckimi znakami w MySQL

  4. WYBIERZ wszystkie rekordy sprzed 30 dni

  5. Jak sprawdzić stan serwera w MySQL Workbench za pomocą GUI