开发者

searching with a name in database for a person - MySQL

In my table I store first name in the fName column and last name in the lName column, now I need to search them with a query, but I don't know the SQL!

exampl开发者_如何学Ce

   lName | fName  
-----------------
Tendulkar| Sachin   
Ganguly  | Sourav   
Khan     | Zaheer  
Dhoni    | Mahendra Singh  

The user should get MAHENDRA SINGH DHONI if he searches for Mahendra Dhoni!


select concat(fName,' ',lName) fullname
from tbl
where concat(' ',fName,' ',lName,' ') like '% Mahendra %'
  and concat(' ',fName,' ',lName,' ') like '% dhoni %'

This will most certainly put to rest any hopes of a well performing query

A variation on the theme

select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%Mahendra%dhoni%'
    or concat(lName,' ',fName) like '%Mahendra%dhoni%')

The 2nd version doesn't care about full part matching, e.g. dhoni will match madhonie


Both of these queries find the name correctly. Note that there are % before and after the name to match, as well as % for every space in the name.

create table tbl (fname varchar(100), lname varchar(100));
insert tbl select 'Mahendra singh', 'dhoni';

select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%Mahendra%dhoni%'
    or concat(lName,' ',fName) like '%Mahendra%dhoni%');

select concat(fName,' ',lName) fullname
from tbl
where (concat(fName,' ',lName) like '%dhoni%Mahendra%'
    or concat(lName,' ',fName) like '%dhoni%Mahendra%');


You are not clear on the nature of the search inputs and specifically the level of flexibility. First, is the user given two boxes for first and last name or only a single search box? If the former, then the fast solution would be:

Select concat( fname, ' ', lname)
From MyTable
Where lname Like 'dhoni%'
    And fname Like 'mahendra%'

The above query only searches for where the first part of the column value begins with the search values. However, if the user can type anything into a single search box, that is harder. If it is presumed that the user has typed <name part> space <name part>, then one solution that solves that specific problem where the user enters only two words is to split on the space and run something like:

Select concat( fname, ' ', lname)
From MyTable
Where ( lname Like '%dhoni%' And fname Like '%mahendra%' )
    Or ( lname Like '%mahendra%' And fname Like '%dhoni%' )

However, that query will perform awful because it forces the system to scan the entire table each time it is executed. Further, what happens when they enter a three part name like Mahendra Singh Dhoni in your search? There are simply too many edge cases for this to be workable IMO. The right solution is to get a full text indexing engine like Lucene that will create a index across both columns and rank the quality of the match.

Lucene


I'm guessing your inputs are from two different textboxes, and hopefully you are using a stored procedure :).

declare @input1 nvarchar(50)
declare @input2 nvarchar(5)
set @input1 = 'Mahendra'
set @input2 = 'Dhoni'

select upper((fname + ' ' + lname)) as 'FullName' 
from customer
where fname like '%' + @input1 + '%'
or fname like '%' + @input1 + '%'
or lname like '%' + @input2 + '%'
or lname like '%' + @input2 + '%'

If you have 1 input box, you will want to split your search term by the space and loop over your search terms against fname and lname columns using the like '%term%' syntax.

Another way would be to make a stored procedure that added fname and lname together and did a soundex match. this is where you match your search term against the sound of the word in the table. Google it should help, its pretty easy.


I am assuming you have 2 inputs and you are only getting part of the first name. If this is true then the following would work:

select concat(fname, ' ', lname)
  from yourtable
 where substring(fname, 1, 5) = 'mahen'
   and lname = 'dhoni'

The advantage of this approach is that it will use the indexes on the column(s) or a combined index whereas the like queries always do full table scans.

You might also check out a search engine like sphinx or solr when you truly do not know the data you are receiving as those are far more flexible that database queries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜