Bukhari Syed
Bukhari Syed

Reputation: 499

MySQL comparing a substring with another String and if it matches get me the match percentage

If I have a column `Full_Names' in table cust and another Column as 'Models' in same table cust. The Full_name column contains ABCD and Models column contain AB then how can I write a query to get Models matches Full_Names 50% since only first two letters matched the whole string.

Thanks in Advance.

Upvotes: 1

Views: 53

Answers (1)

GMB
GMB

Reputation: 222432

You can do:

case when locate(models, full_names) 
    then char_length(models) / char_length(full_name)
    else 0 
end match_ratio

This can be shorten as:

(locate(models, full_names) > 0) * char_length(models) / char_length(full_name)

This gives you a numeric value between 0 and 1 that represents the match ratio. If you want a percentage, you can multiply that by 100/

Upvotes: 1

Related Questions