user12345
user12345

Reputation: 2418

mysql weight based query possible?

Suppose my table is is like.

 id   Name
  1  john 
  2  John Walker Terry
  3  John Terry
  4  Terry

Is it possible, in mysql, to search through name order by most number of sub string matches. If we search John Terry result order should be

John Terry
John Walker Terry
john
Terry

Upvotes: 0

Views: 164

Answers (2)

Blueline
Blueline

Reputation: 408

You could get those results with the following. But this will return anyone with John or Terry in their name so you might want to add some kind of ranking based on the match.

SELECT * FROM yourtable WHERE Name LIKE '%John%' OR Name LIKE '%Terry%'

Upvotes: 1

Manish Jangir
Manish Jangir

Reputation: 505

SELECT * FROM yourtable WHERE NAME LIKE '%John%Terry%'

This query will search in forward direction only not in back. You will get the following result

John Terry John Walker Terry

but all results you are expecting is not possible with sql query.

Upvotes: 0

Related Questions