compare differences in a row between two identical tables in mysql -
i have 2 identical tables different set of data's, compare words in single field against multiple rows of same column in table b , let me know percentage of matches against each id
example:
the following entries in table a
row1: 1, salt water masala row2: 2, water onion maggi milk
the following entries in table b
row1: 1, salt masala water row2: 2, water onion maggi
the desired result
row1: match 100% (all 3 words available different order) row2: match 75% 1 word not match out of 4 words.
it great if me same.
although easier accomplish in application code, possible via couple of mysql functions:
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, ' ') set str1 = replace(str1, ' ', ' '); end while; while instr(str2, ' ') set str2 = replace(str2, ' ', ' '); end while; set @i = 1; set @numwords = 1 + length(str1) - length(replace(str1, ' ', '')); set @nummatches = 0; while @i <= @numwords set @word = string_splitter(str1, ' ', @i); if str2 = @word or str2 concat(@word, ' %') or str2 concat('% ', @word) or str2 concat('% ', @word, ' %') set @nummatches = @nummatches + 1; end if; set @i = @i + 1; end while; return (@nummatches / @numwords) * 100; end // delimiter ;
the first function used in second, 1 want call in code, so:
select percentage_of_matches('salt water masala', 'salt masala water'); select percentage_of_matches('water onion maggi milk', 'water onion maggi');
Comments
Post a Comment