470,625 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,625 developers. It's quick & easy.

Retrieving and sorting names from MySQL

I have a query screen where the user has an option to search by name
fields in the database. There are first, middle and last name fields
and the results returned should be sorted last, first, middle. Here is
the WHERE part of my query:

WHERE CONCAT(Last, First, Middle) >= CONCAT('$_POST[txtSrchLastName]',
'$_POST[txtSrchFirstName]', '$_POST[txtSrchMiddleName]')";

There is an ORDER BY and LIMIT 200 at the end of this and the results
are loaded into a list box.

This seemed to work fine until I ran into a case where the user was
searching for "Allen Coleman". The search returns started with "Stuart
Cole". The "Allen Coleman" record appears down the list a ways. What
is happening is the concatenated database fields "ColeStuart" satisfy
the >= "ColemanAllen" and thus this record is wrongly included.

One solution would be to append spaces to the ends of all 6 of the name
fields, padding them to their full length of 50 characters. But, I'm
not able to find the sort of syntax to use in SQL for padding the
strings to their maximum length.

Or, is there a better solution?

Any hints greatly appreciated!

May 16 '06 #1
5 1319
Rik
hi***********@yahoo.com wrote:
I have a query screen where the user has an option to search by name
fields in the database. There are first, middle and last name fields
and the results returned should be sorted last, first, middle. Here
is the WHERE part of my query:

WHERE CONCAT(Last, First, Middle) >= CONCAT('$_POST[txtSrchLastName]',
'$_POST[txtSrchFirstName]', '$_POST[txtSrchMiddleName]')";

There is an ORDER BY and LIMIT 200 at the end of this and the results
are loaded into a list box.

This seemed to work fine until I ran into a case where the user was
searching for "Allen Coleman". The search returns started with
"Stuart Cole". The "Allen Coleman" record appears down the list a
ways. What is happening is the concatenated database fields
"ColeStuart" satisfy the >= "ColemanAllen" and thus this record is
wrongly included.

One solution would be to append spaces to the ends of all 6 of the
name fields, padding them to their full length of 50 characters.
But, I'm not able to find the sort of syntax to use in SQL for
padding the strings to their maximum length.

Or, is there a better solution?

Yep, don't CONCAT. What's the particular use you had in mind for this
anyway?

SELECT fields
FROM table
WHERE Last >='$_POST[txtSrchLastName]'
AND First >='$_POST[txtSrchFirstName]'
AND Middle >='$_POST[txtSrchMiddleName]'
ORDER BY Last, First, Middle
LIMIT 200

Or if you want to keep your CONCAT:

SELECT CONCAT(Last, First, Middle)
FROM table
WHERE Last >='$_POST[txtSrchLastName]'
AND First >='$_POST[txtSrchFirstName]'
AND Middle >='$_POST[txtSrchMiddleName]'
ORDER BY Last, First, Middle
LIMIT 200

Grtz,
--
Rik Wasmus
May 16 '06 #2

Rik wrote:
Yep, don't CONCAT. What's the particular use you had in mind for this
anyway?

SELECT fields
FROM table
WHERE Last >='$_POST[txtSrchLastName]'
AND First >='$_POST[txtSrchFirstName]'
AND Middle >='$_POST[txtSrchMiddleName]'
ORDER BY Last, First, Middle
LIMIT 200

Or if you want to keep your CONCAT:

SELECT CONCAT(Last, First, Middle)
FROM table
WHERE Last >='$_POST[txtSrchLastName]'
AND First >='$_POST[txtSrchFirstName]'
AND Middle >='$_POST[txtSrchMiddleName]'
ORDER BY Last, First, Middle
LIMIT 200

Grtz,
--
Rik Wasmus


Thanks, Rik.

The application is a database of vital event records (death records in
this case). The users want to be able to enter a name to search for
and then have the list box populated with names, starting with the name
entered to search for, and then proceeding for 200 records, sorted by
last, first, middle.

I considered the solution you have suggested, but am almost certain it
won't fit our needs. For example, if the user searches for last name
"Smith" and first name "Susan", the search will indeed start with Susan
Smith, but as soon as last name "Sorensen" and first name "Anna" is
reached, this record will not be included since "Anna" is not >=
"Susan".

I still can't think of any solution other than padding each of the
names involved (search strings and names returned from the database)
with spaces. Not being very familiar with SQL, I'm not sure how to do
that on the database side.

Any more ideas? Thanks!

May 16 '06 #3
I just figured it out. The following code does just what I need:

WHERE CONCAT(RPAD(Last, 50, ' '),
RPAD(First, 50, ' '),
RPAD(Middle, 50, ' ')) >=
CONCAT(RPAD('$_POST[txtSrchLastName]', 50, ' '),
RPAD('$_POST[txtSrchFirstName]', 50, ' '),
RPAD('$_POST[txtSrchMiddleName]', 50, ' '))";

Thanks again for the response!

May 16 '06 #4
Rik
hi***********@yahoo.com wrote:
The application is a database of vital event records (death records in
this case). The users want to be able to enter a name to search for
and then have the list box populated with names, starting with the
name entered to search for, and then proceeding for 200 records,
sorted by last, first, middle.

I considered the solution you have suggested, but am almost certain it
won't fit our needs. For example, if the user searches for last name
"Smith" and first name "Susan", the search will indeed start with
Susan Smith, but as soon as last name "Sorensen" and first name
"Anna" is reached, this record will not be included since "Anna" is
not >= "Susan".
Why give your users an option to search on that if you don't want that
search executed?
If they want Anna Sorensen, they shouldn't set Firstname.....

Maybe you threw me off by stating:"the user has an option to search by name
fields in the database", while you actually mean: "the user has an option to
pick the starting point for the next 200 records in a sorted list".

If all you want to do is display the next 200 people from a list starting at
a certain name, sorted by Last, First, Middle:

SELECT fields FROM table
WHERE
(Last = $_POST[Last] AND First = $_POST[First] AND Middle=$_POST['Middle']) OR (Last = $_POST[Last] AND First >= $_POST[First])
OR (Last > '$_POST[Last]')
ORDER BY Last, First, Middle
LIMIT 200
I still can't think of any solution other than padding each of the
names involved (search strings and names returned from the database)
with spaces. Not being very familiar with SQL, I'm not sure how to do
that on the database side.

That way:
You include all names with the exact Lastname, from Firstname on, and ALL
Firstnames of following different Lastnames. Effectively only really
searching on Lastname, if that's what you want, implement it like that.

I'd think long and hard how fuzzy you want your search to be, and
specificate EXACTLY how you want your results to be. The current
implementation starts of with a couple of wrong assumptions.

In this case, you might think about SOUNDEX(), LIKE %string% and functions
like that, it depends on what functionality you want to give the user.

Maybe more something like:

SELECT First, Middle, Last, ((Last='$_POST[Last]') + (Middle
='$_POST[Middle]') + (First = '$_POST[First]')) as score
FROM table
WHERE SOUNDEX(Last) = SOUNDEX('$_POST[Last]' OR Last LIKE '%$_POST[Last]'%'
OR
SOUNDEX(First) = SOUNDEX('$_POST[First]' OR First LIKE '%$_POST[First]'%' OR
SOUNDEX(Middle) = SOUNDEX('$_POST[Middle]' OR Middle LIKE
'%$_POST[Middle]'%' OR
ORDER BY score, Last, First, Middle
LIMIT 200

But I'm not a man for fuzzy searches, I assume some people here or more
experienced in that matter.

Grtz,
--
Rik Wasmus
May 16 '06 #5
Rik,

You are correct. What we want is for the user to pick the starting
point in the list of names and then load the next 200. I tried your
suggested method and it works great. It is also noticeably faster in
loading the 200 records than my approach with the RPADs.

Thank you very much! We will use this new method.
Rik wrote:
hi***********@yahoo.com wrote:
The application is a database of vital event records (death records in
this case). The users want to be able to enter a name to search for
and then have the list box populated with names, starting with the
name entered to search for, and then proceeding for 200 records,
sorted by last, first, middle.

I considered the solution you have suggested, but am almost certain it
won't fit our needs. For example, if the user searches for last name
"Smith" and first name "Susan", the search will indeed start with
Susan Smith, but as soon as last name "Sorensen" and first name
"Anna" is reached, this record will not be included since "Anna" is
not >= "Susan".


Why give your users an option to search on that if you don't want that
search executed?
If they want Anna Sorensen, they shouldn't set Firstname.....

Maybe you threw me off by stating:"the user has an option to search by name
fields in the database", while you actually mean: "the user has an option to
pick the starting point for the next 200 records in a sorted list".

If all you want to do is display the next 200 people from a list starting at
a certain name, sorted by Last, First, Middle:

SELECT fields FROM table
WHERE
(Last = $_POST[Last] AND First = $_POST[First] AND Middle
=$_POST['Middle'])

OR (Last = $_POST[Last] AND First >= $_POST[First])
OR (Last > '$_POST[Last]')
ORDER BY Last, First, Middle
LIMIT 200
I still can't think of any solution other than padding each of the
names involved (search strings and names returned from the database)
with spaces. Not being very familiar with SQL, I'm not sure how to do
that on the database side.

That way:
You include all names with the exact Lastname, from Firstname on, and ALL
Firstnames of following different Lastnames. Effectively only really
searching on Lastname, if that's what you want, implement it like that.

I'd think long and hard how fuzzy you want your search to be, and
specificate EXACTLY how you want your results to be. The current
implementation starts of with a couple of wrong assumptions.

In this case, you might think about SOUNDEX(), LIKE %string% and functions
like that, it depends on what functionality you want to give the user.

Maybe more something like:

SELECT First, Middle, Last, ((Last='$_POST[Last]') + (Middle
='$_POST[Middle]') + (First = '$_POST[First]')) as score
FROM table
WHERE SOUNDEX(Last) = SOUNDEX('$_POST[Last]' OR Last LIKE '%$_POST[Last]'%'
OR
SOUNDEX(First) = SOUNDEX('$_POST[First]' OR First LIKE '%$_POST[First]'%' OR
SOUNDEX(Middle) = SOUNDEX('$_POST[Middle]' OR Middle LIKE
'%$_POST[Middle]'%' OR
ORDER BY score, Last, First, Middle
LIMIT 200

But I'm not a man for fuzzy searches, I assume some people here or more
experienced in that matter.

Grtz,
--
Rik Wasmus


May 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by pmz | last post: by
6 posts views Thread by Duderino82 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.