Q:

What is the Most Efficient Way to Query for "Similar" Numeric Values?

Is it possible use a MySQL query to perform this kind of check? If not, what's the most efficient method or algorithm for performing this check in a language like PHP?
I've been tasked with the following:

When entering a new client, check the SSN entered by the user against the clients table to see if there are any other records that have an SSN similar1 to the one entered.
1 Similar is being defined as having a value where 5 of the numbers in the client's SSN in the database match the SSN being entered in both value and position.

Examples of Data That Could be Returned:
Entered Value: 599151223 (From the form)
Matching Values: 533141228
The numbers in bold are numbers that match the entered SSN in both position and value.

A:

I don't think that you can do it with SQL (maybe only with FULL-TEXT search).
The easiest way with php would be something like:
<?php

$ssn = 599151223;

$compare = 533141228;

$ssn = str_split($ssn);
$compare = str_split($compare);

$match_count = 0;

foreach($ssn as $key => $value) {
    if ($compare[$key] == $value)
        $match_count++;
}

if ($match_count == 5) 
    doSomething();

?>

