Click to See Complete Forum and Search --> : SELECT using a LIKE operator


cs3mw
06-08-2007, 08:00 AM
Im trying to create a search function for a client tabble. In my table are two attributes firstname and surname which are seperate. Now what I want my search function to do is when a user types a name into a input box containing both the firstname and surname i want the search to do it on that name now the sql command i currently have is:

$sql = "SELECT * FROM client WHERE firstname like '%$name%' or surname like '%$name%'";

Now in my database I have a firstname Michael and a surname Whiteley, if i type in Mic then the search works fine but if i type in Michael Whiteley the full name then the search comes back as failed. Has anyone any ideas how I can fix this? Cheers

hi_itsme
06-08-2007, 08:27 AM
Hi,
While searching 'Michael Whiteley' is considered as one single word and database searches for it! and there will be no name as 'Michael Whiteley' in firstname...
So split the names with white space and search it will work fine!

bubbisthedog
06-08-2007, 08:56 AM
Or maybe something like this would work:
SELECT * FROM client WHERE firstname||' '||surname like '%$name%'

hi_itsme
06-11-2007, 12:44 AM
Hi,
I tried the above code this was not working for my scenario so i tried to change the above code little bit and code goes like this...
SELECT * FROM client WHERE concat(firstname,' ',surname) like '%$name%' or concat(firstname,surname) like '%name%'
i have concat twice since one may be with space and one without space like 'Michael Whiteley' or 'MichaelWhiteley'