473,513 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1421
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11323
by: Josep | last post by:
Hi, I'd like to poll a database and get the table contents, as well as the field names from that table. I've been to php.net but this time I cannot find something helpful. I can get the data,...
2
1977
by: peter | last post by:
I've got a table which is used for storing votes, each row has a field with a reference to the user who's been voted for and i'd like to simply get the user ref with the most votes,or if tied,...
1
2258
by: Rushabh Dadbhawala | last post by:
Problem: I am fetching data from the database, storing it in a dataSet, and binding it with a Data Grid. The DataGrid controls allows sorting. The problem is that when the data is sorted on one...
10
1680
by: Krakatioison | last post by:
Hi everyone, can someone point me to download of an example for saving and retrieving to/from MYSQL database. Or did anyone of you tried this and could share your code with me. I've got some data...
2
8991
by: pmz | last post by:
Dear Group, I'm connecting in C# with remote (BSD) MySQL server with ODBC Driver, and I'm trying to find the best sollution in such problem: As I've read on MySQL manual, they have suggested...
6
1806
by: Duderino82 | last post by:
I was wondering if there is a way to collect the names of the fields from a specific table. I think the soluction is to be researched in the sql code but maybe someone knows of a way to o so...
4
6135
by: monomaniac21 | last post by:
hi! is it possible to do the aforementioned query - selecting only distinct in 1 col but retrieving all other cols at the same time. regards marc
4
22467
by: Bob | last post by:
Hi all, I've got a table that I've imported and it has junk at the top of the table, so after import I run a delete query to remove the junk lines then I'm left with the field names I want for...
1
7166
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.