V
V
Vyacheslav Belyaev2017-04-04 06:38:28
PHP
Vyacheslav Belyaev, 2017-04-04 06:38:28

How to find similar records in the database by partial occurrence?

Good afternoon Toasters. I have one important question, please help me with it.
There is a customer base. With columns Last name , First name , Middle name , Phone (unique), City .
How can I find records if, for example, last name + first name match, or phone + city or last name + phone. Also, taking into account that the name or patronymic may not be filled.
And it will also be necessary to determine the probability of coincidence. For example, if you found a client by phone number, then this is a high match. If only by last name, then a low coincidence. With a full match of the full name, there is an average match, but if the full name + city matched, then high.
For visual purposes, I will make a list

  • Phone - high match
  • Last name - low match
  • Last name + First name + Middle name - average match
  • Last name+First name+Patronymic name+City - High match

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2017-04-04
@Rsa97

If you need a partial match on the lines with a complete match of individual fields, then something like this (8 and 2 are the weights of the corresponding fields):

SELECT *
  FROM `table`
  ORDER BY (`phone` = :phone) * 8 + 
           (`lastName` = :lastName) * 2 + 
           (`firstName` = :firstName) * 2 + 
           (`middleName` = :middleName) * 2 + 
           (`city` = :city)
    DESC

But the query will not be fast, without the use of indexes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question