Search code examples
arrayspostgresqllevenshtein-distance

Postgresql: levenshtein distance in a row column with comma-separated values


I have a table with the following content :

ID | Name | Alias

1 | William | Will,Willo,Wolli

I would like to return the row ID if the levenshtein distance (or metaphone, it does not matter) of a user-supplied string is lower than a defined threshold for the user name or any of the known aliases.

I know that a possible solution is to use an additional table linking user IDs with user aliases, although I'd like to avoid it if possible.


Solution

  • What you need is string split/explode. It could be done like this:

    SELECT DISTINCT u.id FROM users AS u LEFT JOIN
    (SELECT u.id,unnest(string_to_array(u.alias, ',')) AS ALIAS FROM users AS u) AS q
    ON u.id=q.id
    WHERE levenshtein(u.name,'Jill')<3
    OR levenshtein(q.ALIAS,'Jill')<3;   
    

    http://sqlfiddle.com/#!12/494e6/5