Skip to content
Advertisement

Query to find people with multiple names

I’m working on a fairly big project that allows users of my client’s site to search for people they used to know who might’ve changed their names, primarily due to marriage. Basically, users sign up with all the names they’ve ever used and the data gets entered into a MySQL database. The next step is actually creating the search, which allows users to put in the names they remember someone by.

This is an example registration in the database:

Right now, my query looks like this:

**Substitute out the exact names there for data entered in the search form.

This query doesn’t work because first2 = Frank, which isn’t included in the list. If a person doesn’t know every name someone entered in the database, everything breaks.

However, I don’t think I want to use OR statements, because I don’t want all the results where a name matches Lisa to be returned if someone puts Lisa as just one of the names.

Does anyone know how I could go about doing this?

Advertisement

Answer

You’re constrained unnecessarily here. Consider instead:

Data like:

Lisa Jane Smith, also known as Leese, married and changed name to Lisa Jane Jones

If you present the website user with a “Enter any number of names and indicate their kind” you can then run a query that finds the userids that have all the name kinds mentioned

This can be as simple as dynamically joining, or doing conditional aggregation

Eg if the web user enters “Leese” as firstname and “Jones” as lastname then you can:

Or you can use a join approach (and this for if the web user enters 3 names/kinds):

When you go to form your dynamic queries, don’t concatenate the values into SQL. Just because you’re making a query dynamic built is not an excuse to throw parameterization out of the window. You build the SQL by concatenating together the SQL string with parameter placeholders in and then give the parameters values separately

Pseudocode (i don’t know php) in a language that interpolates its strings with {placeholders}:

You finish up with a sql command that is all parameterized and the parameters are all given values..

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement