473,748 Members | 6,034 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[txtSrchFirstNam e]', '$_POST[txtSrchMiddleNa me]')";

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 >= "ColemanAll en" 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 1433
Rik
hi***********@y ahoo.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[txtSrchFirstNam e]', '$_POST[txtSrchMiddleNa me]')";

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 >= "ColemanAll en" 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[txtSrchFirstNam e]'
AND Middle >='$_POST[txtSrchMiddleNa me]'
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[txtSrchFirstNam e]'
AND Middle >='$_POST[txtSrchMiddleNa me]'
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[txtSrchFirstNam e]'
AND Middle >='$_POST[txtSrchMiddleNa me]'
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[txtSrchFirstNam e]'
AND Middle >='$_POST[txtSrchMiddleNa me]'
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(Las t, 50, ' '),
RPAD(First, 50, ' '),
RPAD(Middle, 50, ' ')) >=
CONCAT(RPAD('$_ POST[txtSrchLastName]', 50, ' '),
RPAD('$_POST[txtSrchFirstNam e]', 50, ' '),
RPAD('$_POST[txtSrchMiddleNa me]', 50, ' '))";

Thanks again for the response!

May 16 '06 #4
Rik
hi***********@y ahoo.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***********@y ahoo.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
11333
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, and retrieve it into an array noproblem. I can get the data+fieldnames and put it into a list.
2
1988
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, return both of them. I cant use subqueries as it's a mysql 4.0 server, any ideas on how i could do it (without retrieving all the nominees then doing select count(*) for all of them then sorting them externaly). thanks, peter
1
2271
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 of the columns in the Data Grid, and I then try to retrieve the selected row using the "SelectedRowIndex", I get the row as per the original order of data. Eg: If the order of rows is: C - A - B, which after sorting becomes A - B - C. Now, if I...
10
1698
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 which needs to be put to mysql database using VB.NET and I can't figure this out. Thanks a lot. K.
2
9001
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 table design including the BLOB-Fieldtype size in UInt64, where they've stored, while inserting, the size of inserted BLOB data. But in the matter of fact, I'm interested if it's necessary to include in table this integer field? Is there any way...
6
1817
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 directly from php. Example. Table: Categories Field1: type1 Field2: type2 Field3: type3 Field4: type4
4
6148
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
22564
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 the table at the top of the table but the field names currently are 'field1' etc, so how do I rename the field names to the fields on the top row of the table. Cheers,
1
7189
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 methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find nothing new or useful contained in this article. Short Review In part one I showed you some...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.