Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Sorting with multiple attributes (order by conformity)

Tags:

sql

sql-server

I've got a table with certain attributes such as name and address. In a search field I want to let users of a system type in whatever they know about these people and return the results in a table. Obviously I do want the results that are fitting best to be on top.

  1. Should the ranking be done in the SQL command or are there other ways that might work better?
  2. Does anyone know a good way to order/rank the results based by highest conformity? I basically do want the person to show up on top that fits the search criteria most.

You can see an example here: http://sqlfiddle.com/#!2/19e9ad

Thanks for your help!

(I do use SQL Server currently, but any help is appreciated.)

like image 402
JKau Avatar asked Apr 12 '26 15:04

JKau


1 Answers

You might want to check the full text indexes and searches - they have options for ranking that might do the job for you:

  • Fulltext index creation info

... and searching functionality info:

  • FREETEXTTABLE
  • CONTAINSTABLE

Alternatively you can also work on your own solution like parsing the user input and matching word-by-word, then boosting results that match more words, exact words ordering etc. But you will need to handle a number of cases yourself (like multi-word cities like 'New York' etc.).

like image 87
Dimitar Kyuchukov Avatar answered Apr 14 '26 08:04

Dimitar Kyuchukov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!