If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Select from 2 fields with one textbox

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-08-11, 04:17
Frankiee Frankiee is offline
Registered User
 
Join Date: Feb 2009
Posts: 6
Select from 2 fields with one textbox

Hi all,

I've got a table Customers, with firstname and surname as fields. On my page I've got a single textbox where it should be possible to search for customers.

The search should be possible in three different ways:

Only firstname (Joe)
Only surname (Smith)
Or both. (Joe Smith)

Can anyone help me with this?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 03-08-11, 04:53
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Be aware that if you have large numbers of entries in the table you should use indexes on first name and last name.
Code:
SELECT firstname,
       lastname
FROM   table
WHERE  firstname = '$input'
        OR lastname = '$input'
        OR Concat(firstname, ' ', lastname) = '$input'
If performance becomes a big issue try the separating the firstname and lastname programmatically and then issue the statement as follows:
Code:
SELECT firstname,
       lastname
FROM   table
WHERE  firstname = '$input'
        OR lastname = '$input'
        OR ( firstname = '$inputfirstname'
             AND lastname = '$inputlastname' )
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com

Last edited by it-iss.com; 03-08-11 at 04:59.
Reply With Quote
  #3 (permalink)  
Old 03-08-11, 05:02
Frankiee Frankiee is offline
Registered User
 
Join Date: Feb 2009
Posts: 6
Quote:
Originally Posted by it-iss.com View Post
Be aware that if you have large numbers of entries in the table you should use indexes on first name and last name.
Code:
SELECT firstname,
       lastname
FROM   table
WHERE  firstname = '$input'
        OR lastname = '$input'
        OR Concat(firstname, ' ', lastname) = '$input'
If performance becomes a big issue try the separating the firstname and lastname programmatically and then issue the statement as follows:
Code:
SELECT firstname,
       lastname
FROM   table
WHERE  firstname = '$input'
        OR lastname = '$input'
        OR ( firstname = '$inputfirstname'
             AND lastname = '$inputlastname' )
That worked perfectly! Thank you
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On