Mysql Array Search Field

Asked

Viewed 30 times

-1

I need to create a search field for my program in BD in Mysql. I want to create a search that lists names of people, but ignoring duplicated letters in names or special characters, such as apostrophes. For example:

I want to search for "Ana Maria" and he return me even if the name is "Anna Maria", or seek "Deltoro" and he return me including "Del'Toro".

If there’s no predefined way to do this, I thought I’d do the search by turning the search string into an array of characters, combined with a function to eliminate the special characters and duplicate letters, and filter the name field in Mysql by this character list. For example:

Busca = 'Anna Maria'
Array = (a,n,m,r,i)

MySQL select * pessoas where... array está dentro do valor do campo nome.

I don’t know if it’s clear, but I have no idea how to build this search in Mysql.

1 answer

-1


RESOLVED:

After a lot of thought, I realized I would move to the SQL search a string with '%' before and after each letter, also replacing spaces, special characters and equal letters with empty strings ('). So I created a function (in PHP case) to adjust:

function str_busca($x) {
        $x_array = str_split($x);
        $nome_ajustado = '';
        foreach ($x_array as $i => $letra) {
            if ($i == 0) {
                $letra = '%'.$letra.'%';
            } else {
                if ($letra == " " or $letra == "-" or $letra == "'" or $letra == $x_array[$i - 1]) {
                    $letra = '';
                } else {
                    $letra .= '%';
                }
            }
            $nome_ajustado .= $letra;
        }
        return $nome_ajustado;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.