'Most matching words in search query

I have user table (e.g. UserMst) is MySQL like

UserID  FirstName  LastName  FullName (expression column)   
------  ---------  --------  ----------------------------
1       Manish     Patel     Manish Patel
2       Dharmesh   Patel     Dharmesh Patel 
3       Patel      Pranay    Patel  Pranay
4       Patel      Ankita    Patel Ankita

Now I am fetching result as follows:

Select * from UserMst
where (    FirstName like 'patel dha%'
        OR LastName  like 'patel dha% 
        OR FullName  like '%patel dha% 
        OR FullName  like '%patel% 
        OR FullName  like '%dha% )
Order by ( case when FirstName like 'patel dha%' then 1
                WHEN LastName  like 'patel dha%' then 2 
                WHEN FirstName like '%patel dha' then 3
                WHEN LastName  like '%patel dha%'then 4 end ),
          FullName   

This query return result perfect, but I need more nearest ordered result. In this case If some one is enter keyword 'patel dha' (here user can leave more space between 2 words as well as he can type as many words too e.g. 'patel dha' or 'patel dha pran') it means he is trying to find/search user 1 (e.g. Dharmesh patel) This records should come first.

My expected result is: (If user enter this keyword: 'patel dha' or 'patel dha')

UserID  FirstName  LastName  FullName   
------  ---------  --------  --------
2   Dharmesh   Patel     Dharmesh Patel 
4   Patel      Ankita    Patel Ankita
3   Patel      Pranay    Patel  Pranay
1   Manish     Patel     Manish Patel

If user search another way then my expected result is: (If user enter this keyword: 'patel dha pr' or 'patel dha pran')

UserID  FirstName  LastName  FullName   
------  ---------  --------  --------
2   Dharmesh   Patel     Dharmesh Patel 
3   Patel      Pranay    Patel  Pranay
4   Patel      Ankita    Patel Ankita
1   Manish     Patel     Manish Patel

Order changed. (Because most matching words found)

Can we use here regular expression? Or maybe another way to do this?



Solution 1:[1]

Actually it works as you said, just that in first example Manish Patel should be ranked 2 because of the Sort order A-Z.

Maybe I didn't get the question fully though :)

Here is a SQLFiddle

EDIT: by the way the entry with the 2 spaces I changed to underscores, since it seemed that SQLFiddle is auto trimming duplicates.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zewa666